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:
See also BaseContainer Manual.
To retrieve and modify String objects stored in a GeData object (GeData type is DA_STRING) respectively use:
See also GeData Manual.
if (object == nullptr)
Definition: c4d_baseobject.h:225
Definition: lib_description.h:330
Definition: c4d_gedata.h:83
const String & GetString(void) const
Definition: c4d_gedata.h:463
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:210
@ 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)
Definition: c4d_string.h:39
- 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:
Copy
A String can be copied with the default operator:
const String content {
"foobar" };
empty = content;
Combine
Multiple String objects can be combined with the usual operators:
const String fooBar = foo + bar;
helloWorld += " world";
Find String
The String class provides functions to find substrings in a given string or character:
const String fooBar =
"fooBar";
static String IntToString(Int32 v)
Definition: c4d_string.h:495
Bool FindFirst(const String &cs, Int32 *pos, Int start=0) const
Definition: c4d_string.h:280
void Py_ssize_t * pos
Definition: dictobject.h:50
maxon::Int32 Int32
Definition: ge_sys_math.h:60
Substrings
The String class provides functions to return substrings of a given string:
String aliceBob(
"Alice and Bob");
aliceBob.Delete(6, 3);
#define iferr_return
Definition: resultbase.h:1465
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:
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:
const String gigawatts =
"1.21";
{
}
Float ParseToFloat(Int32 *error=nullptr, Int unit=0, Int angletype=0, Int base=10) const
Definition: c4d_string.h:441
PyObject * error
Definition: codecs.h:206
maxon::Float Float
Definition: ge_sys_math.h:66
Also a more generic function is available:
From Numbers
Several static functions can be used to convert both 32 and 64 bit numbers to strings:
static String HexToString(UInt32 v, Bool prefix0x=true)
Definition: c4d_string.h:478
maxon::UInt UInt
Definition: ge_sys_math.h:65
Also:
const Float percentage = 0.23;
String FormatNumber(const GeData &val, Int32 format, Int32 fps, Bool bUnit=true)
@ FORMAT_PERCENT
Floating point with % sign, 1.0 = 100%.
Definition: c4d_gui.h:46
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:
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:
const String string {
"foobar" };
Char * GetCStringCopy(STRINGENCODING type=STRINGENCODING::XBIT) const
maxon::Char Char
Definition: ge_sys_math.h:56
void DeleteMem(T *&p)
Definition: defaultallocator.h:257
Filename
The Filename class is used to handle filenames and paths. Its content can be defined with strings and be converted to strings:
See also Filename Manual.
{
}
Manages file and path names.
Definition: c4d_file.h:94
String GetFileString(void) const
Bool FileSelect(FILESELECTTYPE type, FILESELECT flags, const maxon::String &title, const maxon::String &force_suffix=maxon::String())
String GetSuffix(void) const
String GetString(void) const
Date & Time
The class DateTimeParser can be used to parse and create date time strings.
if (dtp == nullptr)
Definition: ge_autoptr.h:37
void GetDateTimeNow(DateTime &t)
Represents a date and time.
Definition: customgui_datetime.h:39
Similar functionality is also provided by functions of the datetime library:
String TimeToString(const DateTime &d, const Bool bShowSeconds=true)
String DateToString(const DateTime &d)
Disc I/O
A String object can be stored in a BaseFile or a HyperFile using:
if (bf == nullptr)
bf->WriteString(string);
bf->Close();
PyCompilerFlags const char * filename
Definition: ast.h:15
@ ANY
Show an error dialog for any error.
if (bf == nullptr)
bf->ReadString(&string);
bf->Close();
@ READ
Open the file for reading.
if (hf == nullptr)
hf->WriteString(string);
hf->Close();
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:
The parser is used with these functions:
if (regEx == nullptr)
String expression(
"(" + anyChar +
"*)(.jpg)");
regEx->Init(expression);
else
#define ANY_CHAR
Any character = ASCII 2.
Definition: lib_regexpr.h:24
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