The NeL framework provides a wealth of string utilities for string manipulation and conversion. Four of the most important features are the toString and fromString methods as well as the CSString and ucstring classes. There are also many useful utility functions that we will also cover.
The string conversion utility really comes down to two components in NeL: a series of overloaded functions called toString() and fromString(). The names of these functions is pretty self-explanatory - the toString() functions convert a parameter to a string value and the fromString() functions convert a string to their native value. Listing all of the available overloads would take too much space but here is a quick demonstration using the nlinfo() logging facility.
void stringConversion()
{
// Used to demonstrate integer conversion.
int myInt = 10;
std::string myStrInt = "15";
// Convert the integer to a string and log it.
nlinfo("myInt's current value is: %s", NLMISC::toString(myInt).c_str());
// The above will log: myInt's current value is: 10
// Now convert the string into the integer and then log it again.
myInt = NLMISC::fromString(myStrInt);
nlinfo("myInt's new value is: %s", NLMISC::toString(myInt).c_str());
// The above will log: myInt's new value is: 15
}
You can see how simple it was to convert a primitive into a string for logging or debugging. But you could also use these facilities in other practical applications - especially the fromString() functions. You could use this to parse user input into native primitive members or variables for use in game logic, for example.
The CSString class is a very useful string utility class. It provides a wealth of utility functions too numerous to name all here and explain each of there uses. It would be better to quickly peruse the API documentation to see all of the methods available. A quick summary however is appropriate. You can create a new version using several versions of the constructor which access C-style char strings, C++ strings and a couple variations of formatted strings.
Once you have a CSString object created you can use it as you would any C++ style string however with the added benefit of several utility methods that allow you to split the string several ways, crop the string, manipulate case and several ways to do case-insensitive searches and compares.
The ucstring class is a variation of the standard C++ string class developed within NeL to support unicode strings. For this reason you will not find the ucstring class in any of the NeL namespaces. Not much explanation it necessary for this since if you know how to use std::string you will know how to use ucstring. There are three important additional methods provided on this class which merit explanation: toString(), fromUtf8() and toUtf8().
There are two variations of the toString() method: one which returns a std::string value and one which takes a std::string argument. The former you would use to convert a ucstring into a std::string for portions of your code which are not unicode-aware. The latter you would use if you need to convert a standard string into a unicode string for portions of code which are strictly unicode.
The fromUtf8() and toUtf8() are used for conversion between UTF8 and UCS2 strings.
For people not using the CSString class there are also a number of functions in the NLMISC namespace for string manipulation. Below is a table of interesting functions for std::string and ucstring users.
bool testWildCard (const char *strIn, const char *wildCard)
Checks strIn using wildCard - a wild-card string. Case-sensitive.
testWildCard("hello world", "hello*");
void splitString(const std::string &str, const std::string &separator, std::vector< std::string> &retList)
Splits str into a vector (retList) of strings using a separator. Also splitUCString() accepts ucstring instead of std::string. Case-sensitive.
std::vector<std::string> foo;
splitString("a:b:c", ":", &foo);
bool strFindReplace(T &str, const T &strFind, const U &strReplace)
For both std::string and ucstring find a sub-string strFind in str and replace instances of it with strReplace. Case-sensitive.
strFindReplace("badString", "bad", "good");
std::string toUpper (const std::string &str)
Also toLower(), same syntax. Converts the entire case of str.
std::string foo = toUpper("foBaR");
std::string stringFromVector (const std::vector< uint8 > &v, bool limited=true)
Converts a vector of bytes into a readable string. Unprintable characters are replaced with '?'.
std::vector<uint8> bar;
// Hi
bar.add(72); bar.add(105);
std::string foo = stringFromVector(&bar);
// foo == "Hi"
std::string bytesToHumanReadable (const std::string &bytes)
Converts a string representing bytes into a human readable string.
std::string foo = bytesToHumanReadable("105123"); // foo is now "102kb"
std::string bytesToHumanReadable (uint32 bytes)
Converts an integer into a human readable string representing bytes.
std::string foo = bytesToHumanReadable(105123); // foo is now "102kb"
uint32 humanReadableToBytes (const std::string &str)
Converts a string with a human readable byte representation into the byte amount.
uint32 bytes = humanReadableToBytes("102kb"); // bytes is now 105123
std::string secondsToHumanReadable (uint32 time)
Converts a number of seconds into a human readable string.
std::string readableTime = secondsToHumanReadable(3600); //readableTime is now "1h"
uint32 fromHumanReadable (const std::string &str)
Converts a human readable time or bytes into a number of seconds.
int bytes = fromHumanReadable("102kb"); // bytes is now 105123
int seconds = fromHumanReadable("1h"); // seconds is now 3600
void explode (const std::string &src, const std::string &sep, std::vector< std::string > &res, bool skipEmpty=false)
Splits a string src by a separator sep into a vector res. The skipEmpty parameter informs explode to not replace the vector if there are no matches.
const std::string myString = "a:b:c:d";
const std::string mySep = ":";
std::vector<std::string> myVec;
explode(myString, mySep, &myVec);