Console.WriteLine(str2); //---'****This is a string'---
str2 = str1.PadRight(20, '*');
Console.WriteLine(str2); //---'This is a string****'---
To trim whitespace from the beginning of a string, the end of a string, or both, you can use the TrimStart()
, TrimEnd()
, or Trim()
instance methods, respectively. The following statements demonstrate the use of these methods:
string str1 = ' Computer ';
string str2;
Console.WriteLine(str1); //---' Computer '---
str2 = str1.Trim();
Console.WriteLine(str2); //---'Computer'---
str2 = str1.TrimStart();
Console.WriteLine(str2); //---'Computer '---
str2 = str1.TrimEnd();
Console.WriteLine(str2); //---' Computer'---
One common operation with string manipulation is splitting a string into smaller strings. Consider the following example where a string contains a serialized series of points:
string str1 = '1,2|3,4|5,6|7,8|9,10';
Each point ('1, 2', '3, 4', and so on) is separated with the | character. You can use the Split()
instance method to split the given string into an array of strings:
string[] strArray = str1.Split('|');
Once the string is split, the result is stored in the string array strArray
and you can print out each of the smaller strings using a foreach
statement:
foreach (string s in strArray) Console.WriteLine(s);
The output of the example statement would be:
1,2
3,4
5,6
7,8
9,10
You can further split the points into individual coordinates and then create a new Point
object, like this:
string str1 = '1,2|3,4|5,6|7,8|9,10';
string[] strArray = str1.Split('|');
foreach (string s in strArray) {
string[] xy= s.Split(',');
Point p = new Point(Convert.ToInt16(xy[0]), Convert.ToInt16(xy[1]));
Console.WriteLine(p.ToString());
}
The output of the above statements would be:
{X=1,Y=2}
{X=3,Y=4}
{X=5,Y=6}
{X=7,Y=8}
{X=9,Y=10}
Occasionally, you need to search for a specific occurrence of a string within a string. For this purpose, you have several methods that you can use.
To look for the occurrence of a word and get its position, use the IndexOf()
and LastIndexOf()
instance methods. IndexOf()
returns the position of the first occurrence of a specific word from a string, while LastIndexOf()
returns the last occurrence of the word. Here's an example:
string str1 = 'This is a long long long string...';
Console.WriteLine(str1.IndexOf('long')); //---10---
Console.WriteLine(str1.LastIndexOf('long')); //---20---
To find all the occurrences of a word, you can write a simple loop using the IndexOf()
method, like this:
int position = -1;
string str1 = 'This is a long long long string...';
do {
position = str1.IndexOf('long', ++position);
if (position > 0) Console.WriteLine(position);
} while (position > 0);
This prints out the following:
10
15
20
To search for the occurrence of particular character, use the IndexOfAny()
instance method. The following statements search the str1
string for the any of the characters a, b, c, d, or e, specified in the char
array:
char[] anyof = 'abcde'.ToCharArray();
Console.WriteLine(str1.IndexOfAny(anyof)); //---8---
To obtain a substring from within a string, use the Substring()
instance method, as the following example shows:
string str1 = 'This is a long string...';
string str2;
Console.WriteLine(str1.Substring(10)); //---long string...---
Console.WriteLine(str1.Substring(10, 4)); //---long---
To find out if a string begins with a specific string, use the StartsWith()
instance method. Likewise, to find out if a string ends with a specific string, use the EndsWith()
instance method. The following statements illustrate this:
Console.WriteLine(str1.StartsWith('This')); //---True---
Console.WriteLine(str1.EndsWith('...')); //---True---
To remove a substring from a string beginning from a particular index, use the Remove() instance method:
str2 = str1.Remove(10);
Console.WriteLine(str2); //---'This is a'---
This statement removes the string starting from index position 10. To remove a particular number of characters, you need to specify the number of characters to remove in the second parameter:
str2 = str1.Remove(10,5); //---remove 5 characters from index 10---
Console.WriteLine(str2); //---'This is a string...'---
To replace a substring with another, use the Replace()
instance method:
str2 = str1.Replace('long', 'short');
Console.WriteLine(str2); //---'This is a short string...'---
To remove a substring from a string without specifying its exact length, use the Replace()
method, like this:
str2 = str1.Replace('long ', string.Empty);