In C, the cstring type does not seem to have any function similar to string. substring (parame) to directly split ion strings from strings, but we can use several functions of cstring to achieve this.
In cstring, find (), delete (), left (), right (), and mid () can be used to split ion strings.
Intfind (tchar ch) const;
Find the given character and return its index number in the string; if it is not found, return-1.
Intdelete (INT nindex, int ncount = 1 );
The return value is the length of the string before the deletion, nindex is the first character index to be deleted, and ncount is to delete several characters at a time. This function is not executed when ncount is too large and there are not enough characters to delete it.
Cstringleft (INT ncount) const;
The returned string is the first ncount character.
Cstringmid (INT nfirst) const;
Cstringmid (INT nfirst, int ncount) const;
Ncount indicates the number of characters to be extracted, and nfirst indicates the start index location to be extracted.
Cstringright (INT ncount) const;
The returned string is the last ncount character.
Example:
Cstringstr ("mynameisrenzheng ");
Cstringstr1, str2, str3;
Intx, Y;
X = Str. Find (_ T ("N"); // 2
Y = Str. Delete (0, 5); // 16
Str1 = Str. Left (3); // myn
Str2 = Str. mid (11, 4); // Zhen. Note that the second parameter indicates the length of the substring.
Str3 = Str. Right (3); // Eng
If cstring STR = _ T ("My name is Ren Zheng ");
The following method can be used to extract substrings by space:
Cstringstr = _ T ("My name is Ren Zheng ");
Carray <cstring, cstring> strarray; // defines a dynamic cstring array to access cstring objects.
While (Str. Find (_ T ("") + 1) // if no space is found,-1 is returned, so here I use it to return the value and Add 1 to the loop.
{
Strarray. add (Str. left (Str. find (_ T (""); // locate the space index, intercept the string on the left of the space, and add it to the strarray dynamic array.
Str. Delete (0, str1. getlength_r () + 1); // return the new string obtained by deleting spaces and the string on the left.
}
Strarray. Add (STR); // Add the remaining strings after deletion;
In this way, the substrings separated by spaces are stored in the strarray object of the dynamic cstring type. Now we can use strarray. get_r (I) to obtain each substring;