About
A String is a variable-length sequence of UTF-32 encoded Unicode characters. The String class provides methods and operators to simplify using character sequences.
- Note
- Cinema 4D strings are fully using UTF-32 Unicode characters. To avoid problems with Unicode try to always use ::Utf32Char unless it is needed to interface with the system or existing libraries.
-
Use ApplicationOutput() to print strings to the Cinema 4D "Console" window.
-
The std::string class of the standard library should only be used if absolutely necessary.
- Warning
- ::String is based on maxon::String. More information on maxon::String is found here: String Manual.
Access
String objects can be created on demand or can be obtained from other entities.
To retrieve and modify String objects stored in a BaseContainer respectively use:
- BaseContainer::GetString(): Returns the String stored at the given ID.
- BaseContainer::SetString(): Sets the String stored at the given ID.
See also BaseContainer Manual.
To retrieve and modify String objects stored in a GeData object (GeData type is ::DA_STRING) respectively use:
- GeData::GetString(): Returns the ::String object.
- GeData::SetString(): Stores the ::String object.
See also GeData Manual.
BaseObject*
const object =
doc->GetActiveObject();
if (object == nullptr)
GeData data;
NONE
Definition: asset_browser.h:1
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:69
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:204
#define ConstDescID(...)
Definition: lib_description.h:592
@ ID_BASELIST_NAME
Definition: obaselist.h:7
const char * doc
Definition: pyerrors.h:226
Strings are also registered in resource files. Each of these strings has an ID in cd4_symbols.h
and is defined in a *
.str file in the string folder of a specific language. This way a plugin can easily be localized by adding str
files for each supported language.
- GeLoadString(): Returns a string from the resource files. Different versions of GeLoadString() allow to replace the placeholders in the string with custom content.
const String & GeLoadString(Int32 id)
- Note
- A plugin must load its resources in PluginMessage() to use GeLoadString(). See Initialize Resources.
If it is needed that the user enters or edits a String a special dialog can be used:
- RenameDialog(): Opens a dialog that presents a String and allows the user to edit this String.
Copy
A String can be copied with the default operator:
- String::operator=(): Assigns the content of the String to the other String object.
const String content { "foobar" };
String empty = "";
empty = content;
Combine
Multiple String objects can be combined with the usual operators:
String foo = "foo";
String bar = "bar";
const String fooBar = foo + bar;
String helloWorld = "hello";
helloWorld += " world";
Find String
The String class provides functions to find substrings in a given string or character:
- String::FindFirst: Searches the string for the first match of a given substring.
- String::FindFirstUpper(): Searches the string for the first match of the upper-case substring.
- String::FindLast(): Searches the string for the last match of the substring.
- String::FindLastUpper(): Searches the string for the last match of the upper-case substring.
const String fooBar = "fooBar";
if (fooBar.FindFirst(
"Bar", &
pos))
void Py_ssize_t * pos
Definition: dictobject.h:50
maxon::Int32 Int32
Definition: ge_sys_math.h:51
Substrings
The String class provides functions to return substrings of a given string:
- String::SubStr(): Returns a substring from the string.
- String::Left(): Returns a substring from the left of the string.
- String::Right(): Returns a substring from the right of the string.
- String::Delete(): Removes a section from the string.
String aliceBob("Alice and Bob");
aliceBob.Delete(6, 3);
#define iferr_return
Definition: resultbase.h:1531
It is also possible to accesses a single character directly:
- String::operator[](): Sets or gets a constant Unicode character from the string at the given position.
string[0] = 'r';
PyObject * string
Definition: asdl.h:6
Compare
The String class provides functions to compare to String objects:
- String::Compare(): Compare the strings and returns their relationship.
- String::ComparePart(): Compares a part of the string.
- String::LexCompare(): Compares the String object with another string and returns their relationship.
- String::LexComparePart(): Compares a part of the string with the given string and returns their relationship
Convert
- Note
- Maxon API data types typically implement a ToString() function.
To Numbers
A string can be converted to a number when the stored characters describe a number. This might be useful for example to handle user input:
- String::ParseToFloat(): Converts the string into a ::Float value.
- String::ParseToInt32(): Converts the string into an ::Int32 value.
- String::ToInt32(): Converts the string to an ::Int32 value.
- String::ToInt64(): Converts the string to an ::Int64 value.
- String::ToUInt32(): Converts the string to a ::UInt32 value.
- String::ToUInt64(): Converts the string to a ::UInt64 value.
- String::ToFloat(): Converts the string to a ::Float value.
- String::ToInt(): Converts the string to an ::Int value.
- String::ToUInt(): Converts the string to a ::UInt value.
const String gigawatts = "1.21";
Float gigawattsFloat = gigawatts.ParseToFloat(&
error);
{
}
PyObject * error
Definition: codecs.h:206
maxon::Float Float
Definition: ge_sys_math.h:57
Also a more generic function is available:
- StringToNumber(): Converts a string to a data value of type ::Float, ::Int32 or ::BaseTime.
From Numbers
Several static functions can be used to convert both 32 and 64 bit numbers to strings:
- String::MemoryToString(): Converts the number to a memory size.
- String::HexToString(): Converts the number to a hexadecimal representation.
- String::IntToString(): Converts the given integer value to a string.
- String::UIntToString(): Converts the given unsigned integer value to a string.
- String::FloatToString(): Converts the given float value to a string.
- String::VectorToString(): Converts the given vector to a string.
maxon::UInt UInt
Definition: ge_sys_math.h:56
Also:
- FormatNumber(): Converts a ::Float, ::Int32 or ::BaseTime to a formatted string.
const Float percentage = 0.23;
@ FORMAT_PERCENT
Floating point with % sign, 1.0 = 100%.
Definition: c4d_gui.h:46
String FormatNumber(const GeData &val, Int32 format, Int32 fps, Bool bUnit=true)
Upper and Lower Case
A typical string operation is to convert all characters to upper or lowercase characters. These functions only work with ANSI characters less than character code 128, all other characters remain unchanged:
- String::ToUpper(): Returns the string converted to upper case characters.
- String::ToLower(): Returns the string converted to lower case characters.
const String string { "foobar" };
C string
Sometimes strings must be communicated with other libraries. In this case it is needed to convert a Cinema 4D String into a standard string:
- String::GetCStringLen(): Gets the expected length of the string after encoding.
- String::GetCString(): Gets the string after encoding.
- String::GetCStringCopy(): Gets the encoded string as a copy.
- String::SetCString(): Sets the string from a given standard C string.
const String string { "foobar" };
Char* cstring =
string.GetCStringCopy();
maxon::Char Char
Definition: ge_sys_math.h:47
void DeleteMem(T *&p)
Definition: defaultallocator.h:269
Filename
The Filename class is used to handle filenames and paths. Its content can be defined with strings and be converted to strings:
- Filename::GetString(): Returns the full filename as a String.
- Filename::SetString(): Sets the filename with the given String.
- Filename::GetFileString(): Returns the file part of the filename as a String.
- Filename::GetSuffix(): Returns the file suffix as a String.
See also Filename Manual.
Filename selectFile;
{
}
LOAD
Load.
Definition: c4d_filterdata.h:1
ANYTHING
Any file.
Definition: ge_prepass.h:0
Date & Time
The class DateTimeParser can be used to parse and create date time strings.
AutoAlloc<DateTimeParser> dtp;
if (dtp == nullptr)
DateTime dt;
void GetDateTimeNow(DateTime &t)
DATE
Date.
Definition: lib_datetimeparser.h:0
TIME
< Time
Definition: lib_datetimeparser.h:2
Similar functionality is also provided by functions of the datetime library:
- ParseTimeString(): Parses the time defined in the given String.
- ParseDateString(): Parses the date defined in the given String.
- FormatTime(): Returns a String with the formatted time. It uses the same arguments as the
asctime()
function of the standard library.
- TimeToString(): Returns a String with the formatted time.
- DateToString(): Returns a String with the formatted date.
- GetMonthName(): Returns a String with the name of the given month.
DateTime dt;
String DateToString(const DateTime &d)
String TimeToString(const DateTime &d, const Bool bShowSeconds=true)
Disc I/O
A String object can be stored in a BaseFile or a HyperFile using:
- BaseFile::ReadString(): Reads a String from the BaseFile.
- BaseFile::WriteString(): Writes a String into the BaseFile.
AutoAlloc<BaseFile> bf;
if (bf == nullptr)
bf->WriteString(string);
bf->Close();
PyCompilerFlags const char * filename
Definition: ast.h:15
WRITE
Problems writing the file.
Definition: ge_prepass.h:4
ANY
Definition: lib_substance.h:28
AutoAlloc<BaseFile> bf;
if (bf == nullptr)
bf->ReadString(&string);
bf->Close();
READ
Problems reading the file.
Definition: ge_prepass.h:3
- HyperFile::ReadString(): Reads a String from the HyperFile.
- HyperFile::WriteString(): Writes a String into the HyperFile.
AutoAlloc<HyperFile> hf;
if (hf == nullptr)
hf->WriteString(string);
hf->Close();
AutoAlloc<HyperFile> hf;
if (hf == nullptr)
hf->ReadString(&string);
hf->Close();
See also BaseFile Manual on String and HyperFile Manual on String.
Regular Expressions
Regular expressions are used to check if a given string matches a certain pattern. The class RegularExprParser provides basic functionality to parse for simple expressions:
- RegularExprParser::Alloc(): Allocates a regular expression parser.
- RegularExprParser::Free(): Frees a regular expression parser.
The parser is used with these functions:
- RegularExprParser::Init(): Initializes the parser with the given regular expression.
- RegularExprParser::FindFirst(): Performs a search using the regular expression passed to RegularExprParser::Init() in the given string and returns the position of the first match.
- RegularExprParser::FindNext(): Returns the position of further matches.
- RegularExprParser::CleanUp(): Cleans the memory allocated by RegularExprParser::Init().
AutoAlloc<RegularExprParser> regEx;
if (regEx == nullptr)
String strSerial;
const String anyChar = String(1,
ANY_CHAR);
String expression("(" + anyChar + "*)(.jpg)");
regEx->Init(expression);
else
#define ANY_CHAR
Any character = ASCII 2.
Definition: lib_regexpr.h:23
CONTAINS
Definition: cpython_raw.h:183
Special Characters
Some symbols may be OS depended. These functions exist to get the correct symbol in each environment:
const Int32 percentage = 55;
String GeGetPercentChar()
Further Reading