If you have used a programming language that has a string data type, you may not be used to it, and others feel the same way, so a few string manipulation functions are provided in the standard C language library. Table 1.3 lists the most commonly used string manipulation functions and their usage instructions. For detailed descriptions and examples of each function, see C + + Builder online Help.
Table 1.3 String manipulation functions
Function description
Strcat () joins the string to the end of the target string
strcmp () compares two strings for equality
Strcmpi () compares two strings for equality, regardless of capitalization
strcpy () copies the string contents to the target string
STRSTR () scans the first occurrence of a string in a string
Strlen () return string length
STRUPR () turns all characters in a string to uppercase
sprintf () establishes strings based on several parameters
The string operation described here is a string processing method in C language. Most C + + compilers provide a CString class that can handle string manipulation (there is a ansistring class in the Visual widget Library of C + + builder that can handle string operations.) The Ansistring class is described in detail in the C + + Builder online Help. Although the C language string processing method is troublesome, but is not outdated, C + + programmers often use the CString class and the Ansistring class and other string classes in the same time using the C language String processing method. You don't want to give an example of each function in a table, just two of the most commonly used functions. The strcpy () function copies a string into another string, which can be either a variable or a direct string. For example, the following code:
Set up a string to hold characters
Char buff[30];
Copy a string literal to the buffer
strcpy (Buff, "This is a test."); /display it
cout << buff << end;
Initialize a second string buffer
Char buff2[]= "A second string."
Copy the contents of this string to the
strcpy (BUFF,BUFF2);
cout << buff << end1;
It is easier to overload the end of a number in a character array than in a numeric array. For example, the following code:
Char buff[10]= "A string";//Later ....
strcpy (Buff, "This is a test."); oops!
This creates a 10 character array that initially specifies a 9-byte string (remember to terminate null). It was possible to forget the length of the array, which would require 16 bytes of string to be copied to the buffer, which overloaded six bytes. This small error erases six bytes of a memory location. So be very careful when copying data into a character array. Another commonly used string function is sprintf (). This function can mix text and numbers to create a formatted string. The following example adds two numbers and then uses sprintf () to establish a string to report the result:
Char buff[20];
int x = 10 * 20;
sprintf (Buff, "The result is:%d", x);
cout << Buff;
When this code segment is executed, the program displays the following results: The result is:200
In this example%d tells the sprintf () function to have an integer value, the end of the format string is inserted into the variable x, telling sprintf () to place the value of the variable x at this position in the string. sprintf () is a special function that can take multiple variables. You must provide the target buffer and the format string, but the variable number following the format string is a Variant. The following sprintf () example uses three additional meta elements:
int x = 20;
int y = 5;
sprintf (Buff, "%d +%d", x, y, x + y);
cout << Buff;
When this code segment is executed, the results displayed on the screen are as follows: 20 + 5 = 25
Describes a single slash in a C + + string that represents a special character. For example, ' \ n ' means a new line character, ' \ t ' represents a jump-table character. To put the actual slash in the string, use double slashes as follows:
strcpy (FileName, "C:\\windows\\system\\win.ini");
Many programmers are struggling to sleep at night because they forget this simple fact. This is a common mistake, don't say I didn't tell you. sprintf () has a brother named wsprintf (), is a Windows version of sprintf (). These two functions may be used in Windows programs at the same time. wsprintf () is similar to sprintf (), and the only difference is that you cannot put floating points in a format string. Both functions are available in C + + builder programs, but it is better to use sprintf () because it fully supports floating-point numbers (you can also enter less than one character). For a further introduction to sprintf (), see C + + Builder online Help.
An array of strings can have either a character array or an array of character arrays (that is, an array of strings). This sounds a bit complicated, in fact, in the previous Argstest program has been used. Such arrays can be assigned the following:
Char strings[][20] = {
"This is String 1",
"This is String 2",
"This is String 3",
"This is string 4"};
This code generates an array of four strings, with a maximum of 19 characters per string. Although you can use this string array, there is a simpler method of string array processing in C + + Builder (described later in C + + builder). Note If you frequently use a string array, you should look at the Standard Template Library (STL). STL provides a way to store and manipulate string arrays more conveniently than C-language character arrays. There is also a string class in the STL.