The Double Backslash

 

String Manipulation

All modern compilers have departed from the old 1960s and 1970s character string concepts that required that each char string be dimensioned at the top of the program in the declarations section (to provide proper storage space on the hard drive) and the char string, when placed in this storage location, be terminated with the NULL character ” the \0 character.

All of the above has been replaced by the ANSIString. The ANSIString uses no termination character. So if there is no null character at the end of the string, how does the compiler know where the string ends?

The answer is simple: For every ANSIString, Visual Studio C# creates a structure with two elements: The first element contains the string itself, and the second element is an integer that notes the length of the string. How simple, and yet so effective. For example, if a string is named strTime, then its length exists and may be found in strTime.Length.

These are the common tasks performed on ANSIStrings:

  • Create a string.

  • Replace one or more characters in the existing string with new characters .

  • Remove characters.

  • Insert characters.

  • Concatenate two or more strings together.

To create a string named newString:

 str newString = "Now is the time for all good men to come to the aid of their country." 

The Replace statement is a global replacement of one character by another character:

 newString = newString.Replace('m', 'x'); // Replace all occurrences of 'm' with 'x'. 

The resulting string is: Now is the tixe for all good xen to coxe to the aid of their country.

Note that the single character is enclosed by single quotes this time (not double quotes). Replace won t work for strings with multiple characters; you cannot do this:

 strBFOS[33] = strBFOS[33].Replace('wow', 'gee'); // Won't work! 

To remove characters:

 str newString = "Now is the time for all good men to come to the aid of their country." newString = newString.Remove(7,4); // Remove 4 chars, beginning at position 7. The first                                    // character position is zero. 

The resulting string is: Now is time for all good men to come to the aid of their country.

Here is how you can insert characters:

 str newString = "Now is the time for all good men to come to the aid of their country." newString = newString.Insert(28,"y"); // Note the double quotes, not single quotes around                                       // the letter y. 

The resulting string is: Now is the time for all goody men to come to the aid of their country.

You can also perform the remove and insert operations in one statement:

 str newString = "Now is the time for all good men to come to the aid of their country." newString = newString.Remove(29,3).Insert(29,"women"); // Remove 'men' and insert 'women'. 

The resulting string is: Now is the time for all good women to come to the aid of their country.

When the C language was first proposed by Bell Laboratories, there was no concatenation scheme included in the package. To combine two strings, the programmer was expected to create a char array with the proper number of characters to hold both the first and the second string, plus one space for the NULL character. Then the characters were transferred from their original strings to the new, larger string, one character at a time. Thank goodness the for loop existed at that time!

Concatenation is easy with Visual Studio C#:

 str newString = "Now is the time"; str newString2 = " for all good people to come to the aid of the party."; newString = newString + newString2; 

Alternative method:

 str newString = "Now is the time"; newString = newString + " for all good people to come to the aid of the party."; 

The resulting string for both methods is: Now is the time for all good people to come to the aid of the party.

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net