C++ Builder Tutorials

C++ Builder: String objects

String is used for declaring objects of the type "String" (note the uppercase S!).
It means the same as "UnicodeString".

Declaration of a String object:

String S;

S is an object, it's not a basic variable such as an int, float or char!


Functions for String objects: String methods

For the examples below, we assume the following declarations were done:

String S = "abcDEF"; 
String S2; int i; bool empt;
Delete(index, count) removes count characters from the String object, starting at position index. Example:
S.Delete(1, 2); // S becomes "cDEF"

Insert(str, index) inserts the string str into the String object, beginning at the position index, where 1 is the first position. Example:
S.Insert("ab", 1); // S becomes "ababcDEF"

Length() returns the number of characters of a String object. Example:
i = S.Length(); // returns 6

IsEmpty() returns true if a String object is empty. Example:
empt = S.IsEmpty(); // returns false

LowerCase() returns a new String that contains all characters converted to lowercase. Example:
S2 = S.LowerCase(); // returns "abcdef"

Pos(substr) returns the position at which a substring begins, where 1 is the index of the first character, and so on. If the substring is not contained in the original string, Pos() returns 0. Example:
i = S.Pos("abc"); // returns 1

SubString(index, count) returns a new String, that contains count characters beginning at index. Example:
S2 = S.SubString(2, 3); // returns "bcD"

Trim() returns a new String, removing leading and trailing spaces and control characters. Example:
S = Edit1->Text;
S = S.Trim();
i = S.Length(); // number of characters in Edit1, excluding spaces and tabs


TrimLeft() returns a new String, removing leading spaces and control characters.

TrimRight() returns a new String, removing trailing spaces and control characters.

UpperCase() returns a new String that contains all characters converted to uppercase. Example:
S2 = S.UpperCase(); // returns "ABCDEF"