String Manual
maxon::String is the universal string class of the MAXON API . Is is based on maxon::StringInterface and maxon::StringTemplate .
A maxon::String can easily be created on the stack and constructed using the "+=" operator:
// create the string maxon::String message { "Hello" };
// extend the string message += maxon::String ( " " ); message += "World!" _s;
A maxon::String can also be constructed with:
// create string maxon::String message;
// init message.Init(2, '9' ) iferr_return ;
// append message.Append( " bottles of beer on the wall" _s) iferr_return ; message.AppendChar( '!' ) iferr_return ;
Integer values can be added to the maxon::String with:
A maxon::String can also be constructed with FormatString() .
// This example function creates a new String based on the input data. static maxon::String FormatObjectData( const maxon::String objectName, maxon::Int polyCount, maxon::Int pointCount) { const maxon::String res = FormatString ( "Object \"@\" (@ Polygons, @ Points)" , objectName, polyCount, pointCount); return res; }A given maxon::String can easily printed to the debug console with DiagnosticOutput() or similar functions. See Debug and Output Functions .
It is easily possible to iterate over all characters stored in a given maxon::String :
// some string const maxon::String message { "Hello World!" };
// get each character for ( const maxon::Utf32Char & c : message) { maxon::String str; str.AppendChar(c) iferr_return ; DiagnosticOutput ( "Character: @ (@)" , str, c); }
The content of the maxon::String can also be accessed with these functions:
// get each character for ( maxon::Int i = 0; i < length; ++i) { // get character const maxon::Utf32Char c = message[i]; maxon::String str; str.AppendChar(c) iferr_return ; DiagnosticOutput ( "Character: @ (@)" , str, c); } }
A maxon::String can be edited with:
A maxon::String can be converted to many other data formats but it also can be constructed by converting another data type to a maxon::String .
A maxon::String can be converted to a upper-case or lower-case version of itself:
另请参阅 MAXON_CONSTEXPR_TOLOWER .
// This example turns the given string into a lower-case string. const maxon::String originalMessage { "Hello World!" }; const maxon::String lowerMessage = originalMessage.ToLower(); DiagnosticOutput (lowerMessage);若 maxon::String describes a numeric value, this value can be converted to a numeric data type:
Also numbers can be converted to a maxon::String :
另请参阅 maxon::StringConversion .
// This example converts the given number to a string representation. const maxon::String value = maxon::String::FloatToString( maxon::Float (123.4)); maxon::String message { "Value: " }; message += value;
If needed, a
maxon::String
can be converted to a C-string. This might be useful to convert it into
std:string
.
The content of the maxon::String can also be converted into special encodings:
Values of enumeration classes can be converted to maxon::String using a function template defined with the MAXON_ENUM_LIST / MAXON_ENUM_ORDERED_LIST attributes:
// This example compares two strings. The COMPARERESULT // is converted to a string to print it to the console.// compare strings const maxon::COMPARERESULT res = textA.Compare(textB);
// print compare result const maxon::String resStr = maxon::ToString<maxon::FormatStatement>(res, nullptr , false ); DiagnosticOutput ( "The string is @" , resStr);
Further functions are:
A given maxon::String can be compared with another maxon::String :
The comparison modes are:
A defined substring can be obtained from a given maxon::String .
Often it is useful to remove special characters like space, tab, line feed etc. from a maxon::String .
// remove spaces message.Trim() iferr_return ;
// break down into sentences maxon::BaseArray<maxon::String> sentences; message.Split( "." _s, true , sentences) iferr_return ; for ( const maxon::String & sentence : sentences) DiagnosticOutput (sentence);
A typical string operation is to check if a given maxon::String contains given substring.
// check if http:// maxon::Int pos = NOTOK ; const maxon::String http( "http://" ); if (webAdress.Find(http, &pos)) { maxon::String domain = webAdress.GetPart(http.GetLength(), maxon::StringEnd ()); webAdress = "https://" _s; webAdress.Append(domain) iferr_return ; }
Further utility functions are:
Substring functions use maxon::StringPosition and maxon::StringCount to define the needed arguments.
A StringPosition object defines a position within a maxon::String .
A StringCount object defines a range of characters within a maxon::String .