String Manipulation in c++ for beginers
Convenience STRING manipulation Tutorial. ideal for beginners debugging After getting my self reaquainted with c++ I remembered just how much of a pain strings can be. UE4 gives us FString, FName a...
Convenience STRING manipulation Tutorial. ideal for beginners debugging
After getting my self reaquainted with c++ I remembered just how much of a pain strings can be.
UE4 gives us FString, FName and FText to work with for various scenarios, and I recomend reading up on them, its important info for speed of calculation and localisation. Nonetheless the standard string (std::string) still has uses, especialy as a debugging tool. These files should allow you to work with strings in a manner similar to unreal script or other scripting languages. I wrote the following code to facilitate simple on screen debugging, and thought Id share it with the comunity.
Example uses: PrintStr("some text about mouse X:" + ToString(MouseLocation.X) + " Y:" + ToString(MouseLocation.Y));
Add code to your project (select no base class) - name AdvString in order to use GEngine you need to include Engine.h in your main project file, Projects created in later versions only include Engineminimal.h ' HEADER'
// File: AdvString.h
#include
#include
#include
#include
#include "Spiral.h"
class BadConversion : public std::runtime_error {
public:
BadConversion(std::string const& s)
: std::runtime_error(s)
{ }
};
inline std::string ToString(double x)
{
std::ostringstream o;
if (!(o < x))
throw BadConversion("ToString(double)");
return o.str();
}
inline std::string ToString(float x)
{
std::ostringstream o;
if (!(o < x))
throw BadConversion("ToString(float)");
return o.str();
}
inline std::string ToString(int x)
{
std::ostringstream o;
if (!(o < x))
throw BadConversion("ToString(int)");
return o.str();
}
inline std::string ToString(short x)
{
std::ostringstream o;
if (!(o < x))
throw BadConversion("ToString(short)");
return o.str();
}
inline std::string ToString(long x)
{
std::ostringstream o;
if (!(o < x))
throw BadConversion("ToString(long)");
return o.str();
}
inline std::string ToString(bool x)
{
if (x)
{
return "true";
}
else
{
return "false";
}
}
inline FString ToFString(std::string x)
{
FString f(x.c_str());
return f;
}
inline void PrintFStr(FString text)
{
GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::White, text);
}
inline void PrintFStrRed(FString text)
{
GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Red, text);
}
inline void PrintFStrGreen(FString text)
{
GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, text);
}
inline void PrintFStrBlue(FString text)
{
GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Blue, text);
}
inline void PrintStr(std::string text)
{
GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::White, ToFString(text));
}
inline void PrintStrRed(std::string text)
{
GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Red, ToFString(text));
}
inline void PrintStrGreen(std::string text)
{
GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, ToFString(text));
}
inline void PrintStrBlue(std::string text)
{
GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Blue, ToFString(text));
}
source
#include "YOUR MAIN CLASS.h"
#include "AdvString.h"
// inline code defined in .h