String in C#

The String Type

 

One of the best examples of how class members can replace built-in functions is found with strings. In the past, every language has defined its own specialized functions for string manipulation. In .NET, however, you use the methods of the String class, which ensures consistency between all .NET  languages.

The following code snippet shows several ways to manipulate a string using its object nature:

string myString = "This is a test string ";

myString = myString.Trim(); // = "This is a test string"

myString = myString.Substring(0, 4); // = "This"

myString = myString.ToUpper(); // = "THIS"

myString = myString.Replace("IS", "AT"); // = "THAT"

int length = myString.Length; // = 4

The first few statements use built-in methods, such as Trim(), Substring(), ToUpper(), and Replace(). These methods generate new strings, and each of these statements replaces the current myString with the new string object. The final statement uses a built-in Length property,which returns an integer that represents the number of characters in the string.

Note that the Substring() method requires a starting offset and a character length. Strings use zero-based counting. This means that the first letter is in position 0, the second letter is in position 1, and so on. You'll find this standard of zero-based counting throughout the .NET Framework for the sake of consistency. You've already seen it at work with arrays. You can even use the string methods in succession in a single (rather ugly) line:

myString = myString.Trim().Substring(0, 4).ToUpper().Replace("IS", "AT");

Or, to make life more interesting, you can use the string methods on string literals just as easily as string variables:

myString = "hello".ToUpper(); // Sets myString to "HELLO"

Table  lists some useful members of the System.String class.

Member

Description

Length

Returns the number of characters in the string (as an integer).

ToUpper() and ToLower()

Returns a copy of the string with all the characters changed to uppercase or lowercase characters.

Trim(), TrimEnd(), and TrimStart()

Removes spaces or some other characters from either (or both) ends of a string.

PadLeft() and PadRight()

 

Adds the specified character to the appropriate side of a string, as many times as necessary to make the total length of the string equal to the number you specify. For example, "Hi".PadLeft(5, '@') returns the string "@@@Hi".

Insert()

Puts another string inside a string at a specified (zero-based)

index position. For example, Insert(1, "pre") adds the string  pre after the first character of the current string.

Remove()

Removes a specified number of strings from a specified

position. For example, Remove(0, 1) removes the first character.

Replace()

Replaces a specified substring with another string. For example, Replace("a", "b") changes all a characters in a string into b characters.

Substring()

 

Extracts a portion of a string of the specified length at the specified location (as a new string). For example,Substring(0, 2) retrieves the first two characters.

StartsWith() and EndsWith()

Determines whether a string starts or ends with a specified substring. For example, StartsWith("pre") will return either

true or false, depending on whether the string begins with the letters pre in lowercase.

IndexOf() and LastIndexOf()

Finds the zero-based position of a substring in a string. This

returns only the first match and can start at the end or beginning. You can also use overloaded versions of these methods that accept a parameter that specifies the position to start the search.

Split()

Divides a string into an array of substrings delimited by a specific substring. For example, with Split(".") you could chop a paragraph into an array of sentence strings.

Join()

Fuses an array of strings into a new string. You can also specify a separator that will be inserted between each element.




Shashi Ray