-
首页
-
C4D R23.110 C++ SDK
String
Manual (Classic)
内容表
关于
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.
-
注意
-
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.
-
使用
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.
-
警告
-
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 Manual
.
To retrieve and modify
String
objects stored in a
GeData
object (
GeData
type is
DA_STRING
) respectively use:
另请参阅
GeData Manual
.
// This example reads the name of the given BaseObject using the "Name" parameter.
BaseObject
*
const
object
= doc->
GetActiveObject
();
if
(
object
==
nullptr
)
return
maxon::IllegalArgumentError(
MAXON_SOURCE_LOCATION
);
GeData
data;
if
(!object->
GetParameter
(
DescID
(
ID_BASELIST_NAME
), data,
DESCFLAGS_GET::NONE
))
return
maxon::UnexpectedError(
MAXON_SOURCE_LOCATION
);
ApplicationOutput
(
"Active Object: "
+ data.
GetString
());
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.
// This example loads a string from the plugin's string resources.
const
String
string
=
GeLoadString
(IDS_TESTSTRING);
ApplicationOutput
(
string
);
-
注意
-
A plugin must load its resources in
PluginMessage()
to use
GeLoadString()
。见
初始化资源
.
If it is needed that the user enters or edits a
String
a special dialog can be used:
拷贝
A
String
can be copied with the default operator:
// This example copies a String.
const
String
content {
"foobar"
};
String
empty =
""
;
empty = content;
ApplicationOutput
(empty);
Combine
Multiple
String
objects can be combined with the usual operators:
// This example merges two strings using
// different operators.
String
foo =
"foo"
;
String
bar =
"bar"
;
const
String
fooBar = foo + bar;
ApplicationOutput
(fooBar);
String
helloWorld =
"hello"
;
helloWorld +=
" world"
;
ApplicationOutput
(helloWorld);
Find String
The
String
class provides functions to find substrings in a given string or character:
// This example searches for a substring in the given string
// and prints the position of the first occurrence.
const
String
fooBar =
"fooBar"
;
Int32
pos = 0;
// search for substring "Bar"
if
(fooBar.
FindFirst
(
"Bar"
, &pos))
ApplicationOutput
(
"Found at position "
+
String::IntToString
(pos));
Substrings
The
String
class provides functions to return substrings of a given string:
// This example creates various substrings of the given String and changes it.
String
aliceBob(
"Alice and Bob"
);
// prints "Alice"
ApplicationOutput
(aliceBob.Left(5));
// prints "Bob"
ApplicationOutput
(aliceBob.Right(3));
// prints "and"
ApplicationOutput
(aliceBob.SubStr(6, 3));
// removes "and"
aliceBob.Delete(6, 3);
// adds a "+"
aliceBob.Insert(6,
'+'
)
iferr_return
;
// prints "Alice + Bob"
ApplicationOutput
(aliceBob);
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.
// This example accesses a single character directly.
String
string(
"define"
);
string
[0] =
'r'
;
ApplicationOutput
(
string
);
比较
The
String
class provides functions to compare to
String
objects:
转换
-
注意
-
MAXON API
data types typically implement a
ToString()
函数。
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:
// This example converts the given String into a Float value.
const
String
gigawatts =
"1.21"
;
Int32
error = 0;
Float
gigawattsFloat = gigawatts.
ParseToFloat
(&error);
if
(error != 0)
{
// could not convert string to float
}
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:
// This example prints the address and size of the given memory block to the console.
const
UInt
memoryAdress = (
UInt
)memory;
ApplicationOutput
(
"Memory adress: "
+
String::HexToString
(memoryAdress));
Also:
// This example prints the given float value as a percentage including the "%" symbol.
const
Float
percentage = 0.23;
const
String
formattedString =
FormatNumber
(percentage,
FORMAT_PERCENT
, 24);
ApplicationOutput
(formattedString);
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:
// This example converts the given String to upper case characters.
const
String
string
{
"foobar"
};
ApplicationOutput
(
string
.ToUpper());
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:
// This example converts the given String into a C-String to use it with the standard library.
const
String
string
{
"foobar"
};
// convert to c string and insert into std string.
Char
* cstring =
string
.
GetCStringCopy
();
std::string stdString { cstring };
DeleteMem
(cstring);
Filename
The
Filename
class is used to handle filenames and paths. Its content can be defined with strings and be converted to strings:
另请参阅
Filename Manual
.
// This example opens the file select dialog and prints the path,
// the filename and suffix of the selected file.
Filename
selectFile;
// open file selector dialog
if
(selectFile.
FileSelect
(
FILESELECTTYPE::ANYTHING
,
FILESELECT::LOAD
,
"Select File"
_s))
{
ApplicationOutput
(
"File selected: "
+ selectFile.
GetString
());
ApplicationOutput
(
"File: "
+ selectFile.
GetFileString
());
ApplicationOutput
(
"Suffix: "
+ selectFile.
GetSuffix
());
}
Date & Time
类
DateTimeParser
can be used to parse and create date time strings.
// This example defines the formatting date and time
// and prints the current date and time to the console.
AutoAlloc<DateTimeParser>
dtp;
if
(dtp ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
dtp->
SetFormatString
(
"YYYY-MM-DD"
,
DATETIMEPARSERMODE::DATE
);
dtp->
SetFormatString
(
"HH-MM-SS"
,
DATETIMEPARSERMODE::TIME
);
DateTime
dt;
GetDateTimeNow
(dt);
const
String
date = dtp->
MakeString
(dt,
DATETIMEPARSERMODE::DATE
);
const
String
time = dtp->
MakeString
(dt,
DATETIMEPARSERMODE::TIME
);
ApplicationOutput
(date +
" "
+ time);
Similar functionality is also provided by functions of the datetime library:
// This example uses the simple functions to print the current date and time to the console.
DateTime
dt;
GetDateTimeNow
(dt);
ApplicationOutput
(
DateToString
(dt) +
" "
+
TimeToString
(dt));
Disc I/O
A
String
object can be stored in a
BaseFile
或
HyperFile
using:
// This example writes the string into a new BaseFile.
AutoAlloc<BaseFile>
bf;
if
(bf ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
// open BaseFile to write
if
(!bf->
Open
(filename,
FILEOPEN::WRITE
,
FILEDIALOG::ANY
))
return
maxon::UnknownError(
MAXON_SOURCE_LOCATION
);
bf->
WriteString
(
string
);
bf->
关闭
();
// This example reads a String form the given BaseFile.
AutoAlloc<BaseFile>
bf;
if
(bf ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
// open BaseFile to read
if
(!bf->
Open
(filename,
FILEOPEN::READ
,
FILEDIALOG::ANY
))
return
maxon::UnknownError(
MAXON_SOURCE_LOCATION
);
String
string;
bf->
ReadString
(&
string
);
ApplicationOutput
(
"String from BaseFile: "
+
string
);
bf->
关闭
();
// This example writes the string into a new HyperFile.
AutoAlloc<HyperFile>
hf;
if
(hf ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
// open HyperFile to write
if
(hf->
Open
(789, filename,
FILEOPEN::WRITE
,
FILEDIALOG::ANY
))
return
maxon::UnknownError(
MAXON_SOURCE_LOCATION
);
hf->
WriteString
(
string
);
hf->
关闭
();
// This example reads a String from the given HyperFile.
AutoAlloc<HyperFile>
hf;
if
(hf ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
// open HyperFile to read
if
(!hf->
Open
(789, filename,
FILEOPEN::READ
,
FILEDIALOG::ANY
))
return
maxon::UnknownError(
MAXON_SOURCE_LOCATION
);
String
string;
hf->
ReadString
(&
string
);
ApplicationOutput
(
"String from HyperFile: "
+
string
);
hf->
关闭
();
另请参阅
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:
// This example checks if the given String is a filename containing the "jpg" suffix.
// If so, the full name is printed to the console.
AutoAlloc<RegularExprParser>
regEx;
if
(regEx ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
String
strSerial;
Int32
pos;
// define expression
const
String
anyChar =
String
(1,
ANY_CHAR
);
String
expression(
"("
+ anyChar +
"*)(.jpg)"
);
regEx->
Init
(expression);
// check string
if
(regEx->
FindFirst
(filename,
REGPARSEMODE::CONTAINS
,
false
, pos, strSerial))
ApplicationOutput
(strSerial);
else
ApplicationOutput
(
"Expression does not match."
);
Special Characters
Some symbols may be OS depended. These functions exist to get the correct symbol in each environment:
// This example prints the given percentage value using the "%" symbol.
const
Int32
percentage = 55;
ApplicationOutput
(
"Percentage: "
+
String::IntToString
(percentage) +
GeGetPercentChar
());
延伸阅读
static String HexToString(UInt32 v, Bool prefix0x=true)
定义:
c4d_string.h:478
Bool Init(const String &strExprssion)
Bool WriteString(const maxon::String &v)
Bool ReadString(maxon::String *v)
String FormatNumber(const GeData &val, Int32 format, Int32 fps, Bool bUnit=true)
定义:
lib_description.h:327
String GetSuffix(void) const
@ ANY
Show an error dialog for any error.
#define ANY_CHAR
Any character = ASCII 2.
定义:
lib_regexpr.h:24
@ FORMAT_PERCENT
Floating point with % sign, 1.0 = 100%.
定义:
c4d_gui.h:40
maxon::Float Float
定义:
ge_sys_math.h:64
void SetFormatString(const String &sFormat, DATETIMEPARSERMODE mode)
Char * GetCStringCopy(STRINGENCODING type=STRINGENCODING::XBIT) const
@ CONTAINS
Partial match.
Manages file and path names.
定义:
c4d_file.h:93
void GetDateTimeNow(DateTime &t)
Bool FindFirst(const String &cs, Int32 *pos, Int start=0) const
定义:
c4d_string.h:280
#define iferr_return
定义:
resultbase.h:1434
#define MAXON_SOURCE_LOCATION
定义:
memoryallocationbase.h:66
@ ID_BASELIST_NAME
定义:
obaselist.h:7
Bool Open(const Filename &name, FILEOPEN mode=FILEOPEN::READ, FILEDIALOG error_dialog=FILEDIALOG::IGNOREOPEN, BYTEORDER order=BYTEORDER::V_MOTOROLA, Int32 type='C4DC', Int32 creator='C4D1')
String MakeString(const DateTime &dt, DATETIMEPARSERMODE mode)
Bool Open(Int32 ident, const Filename &filename, FILEOPEN mode, FILEDIALOG error_dialog)
String GetString(void) const
static String IntToString(Int32 v)
定义:
c4d_string.h:495
Float ParseToFloat(Int32 *error=nullptr, Int unit=0, Int angletype=0, Int base=10) const
定义:
c4d_string.h:441
Represents a date and time.
定义:
customgui_datetime.h:38
const String & GeLoadString(Int32 id)
void DeleteMem(T *&p)
定义:
defaultallocator.h:258
String GetFileString(void) const
maxon::Int32 Int32
定义:
ge_sys_math.h:58
Bool ReadString(maxon::String *v)
Bool WriteString(const maxon::String &v)
#define ApplicationOutput(formatString,...)
定义:
debugdiagnostics.h:207
String DateToString(const DateTime &d)
const String & GetString(void) const
定义:
c4d_gedata.h:463
@ READ
Open the file for reading.
String GeGetPercentChar()
String TimeToString(const DateTime &d, const Bool bShowSeconds=true)
BaseObject * GetActiveObject(void)
maxon::UInt UInt
定义:
ge_sys_math.h:63
maxon::Char Char
定义:
ge_sys_math.h:54
Bool GetParameter(const DescID &id, GeData &t_data, DESCFLAGS_GET flags)
Bool FindFirst(const String &strText, REGPARSEMODE mode, Bool bOnlyFirst, Int32 &lPos, String &strPattern)
Bool FileSelect(FILESELECTTYPE type, FILESELECT flags, const maxon::String &title, const maxon::String &force_suffix=maxon::String())