String Manipulation in c++ for beginers - Epic Wiki

# 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 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

  1. // File: AdvString.h

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include "Spiral.h"

  7. class BadConversion : public std::runtime_error {

  8. public:

  9. BadConversion(std::string const& s)
    
  10. 	: std::runtime\_error(s)
    
  11. { }
    
  12. };

  13. inline std::string ToString(double x)

  14. {

  15. std::ostringstream o;
    
  16. if (!(o << x))
    
  17. 	throw BadConversion("ToString(double)");
    
  18. return o.str();
    
  19. }

  20. inline std::string ToString(float x)

  21. {

  22. std::ostringstream o;
    
  23. if (!(o << x))
    
  24. 	throw BadConversion("ToString(float)");
    
  25. return o.str();
    
  26. }

  27. inline std::string ToString(int x)

  28. {

  29. std::ostringstream o;
    
  30. if (!(o << x))
    
  31. 	throw BadConversion("ToString(int)");
    
  32. return o.str();
    
  33. }

  34. inline std::string ToString(short x)

  35. {

  36. std::ostringstream o;
    
  37. if (!(o << x))
    
  38. 	throw BadConversion("ToString(short)");
    
  39. return o.str();
    
  40. }

  41. inline std::string ToString(long x)

  42. {

  43. std::ostringstream o;
    
  44. if (!(o << x))
    
  45. 	throw BadConversion("ToString(long)");
    
  46. return o.str();
    
  47. }

  48. inline std::string ToString(bool x)

  49. {

  50. if (x)
    
  51. {
    
  52. 	return "true";
    
  53. }
    
  54. else
    
  55. {
    
  56. 	return "false";
    
  57. }
    
  58. }

  59. inline FString ToFString(std::string x)

  60. {

  61. FString f(x.c\_str());
    
  62. return f;
    
  63. }

  64. inline void PrintFStr(FString text)

  65. {

  66. GEngine\-\>AddOnScreenDebugMessage(\-1, 1.5, FColor::White, text);
    
  67. }

  68. inline void PrintFStrRed(FString text)

  69. {

  70. GEngine\-\>AddOnScreenDebugMessage(\-1, 1.5, FColor::Red, text);
    
  71. }

  72. inline void PrintFStrGreen(FString text)

  73. {

  74. GEngine\-\>AddOnScreenDebugMessage(\-1, 1.5, FColor::Green, text);
    
  75. }

  76. inline void PrintFStrBlue(FString text)

  77. {

  78. GEngine\-\>AddOnScreenDebugMessage(\-1, 1.5, FColor::Blue, text);
    
  79. }

  80. inline void PrintStr(std::string text)

  81. {

  82. GEngine\-\>AddOnScreenDebugMessage(\-1, 1.5, FColor::White, ToFString(text));
    
  83. }

  84. inline void PrintStrRed(std::string text)

  85. {

  86. GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Red, ToFString(text));

  87. }

  88. inline void PrintStrGreen(std::string text)

  89. {

  90. GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, ToFString(text));

  91. }

  92. inline void PrintStrBlue(std::string text)

  93. {

  94. GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Blue, ToFString(text));

  95. }

source

  1. #include "YOUR MAIN CLASS.h"

  2. #include "AdvString.h"

  3. // inline code defined in .h

Retrieved from "https://wiki.unrealengine.com/index.php?title=String_Manipulation_in_c%2B%2B_for_beginers&oldid=8888"

Category: