Saturday, February 11, 2012

4.4.6. Other string manipulations

We have already discussed that strings in C# are immutable, can not be changed. But we can perform operations on them to get a new chain. For example:

  • ToUpper() converts to uppercase: correctName = name.ToUpper();
  • ToLower() converts to lowercase: password2 = password.ToLower();
  • Insert (int position, string substring): Inserts a substring in a certain position of the original string: newName = name.Insert(0, "Mr. ");
  • Remove (int position, int amount): Removes a number of characters in a certain position: surname = fullName.Remove (0,6);
  • Replace (string textToFind, string newText): Replaces a string (every time it is found) with another: correctName =name.Replace("Bill", "William");

A program to try all these possibilities might be:

/*---------------------------*/
/* C# Example #43: */
/* example43.cs */
/* */
/* Text strings (2) */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/

using System;

public class Example43
{

public static void Main()
{

string example = "Hi! How are you?";
Console.WriteLine("The text is: {0}",
example);
Console.WriteLine("The first letter is: {0}",
example[0]);
Console.WriteLine("The first three letters: {0}",
example.Substring(0,3));

Console.WriteLine("The length of the string is: {0}",
example.Length);
Console.WriteLine("\"are\" is at position: {0}",
example.IndexOf("are"));
Console.WriteLine("The last H is at position: {0}",
example.LastIndexOf("H"));
Console.WriteLine("Uppercase: {0}",
example.ToUpper());
Console.WriteLine("Lower case: {0}",
example.ToLower());
Console.WriteLine("If we insert \", man\": {0}",
example.Insert(2,", man"));
Console.WriteLine("If we remove the first 4 letters: {0}",
example.Remove(0, 4));
Console.WriteLine("If we change YOU to THINGS GOING: {0}",
example.Replace("you", "things going"));
}
}


And its result would be

The text is: Hi! How are you?
The first letter is: H
The first three letters: Hi!
The length of the string is: 16
"are" is at position: 8
The last H is at position: 4
Uppercase: HI! HOW ARE YOU?
Lower case: hi! how are you?
If we insert ", man": Hi, man! How are you?
If we remove the first 4 letters: How are you?
If we change YOU to THINGS GOING: Hi! How are things going?


Another interesting possibility, though a bit more advanced, is breaking a string into pieces, which are separated by a set of delimiters (eg spaces or commas). To achieve this, we can use "Split", which creates an array from the fragments of the chain, like this:

/*---------------------------*/
/* C# Example #43b: */
/* example43b.cs */
/* */
/* Text strings (2b) */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/

using System;

public class Example43b
{

public static void Main()
{

string example = "one,two.three,four";
char [] delimiters = {',', '.'};
int i;
string [] splitExample = example.Split(delimiters);
for (i=0; i<splitExample.Length; i++)
Console.WriteLine("Fragment {0}= {1}",
i, splitExample[i]);
}
}

Which would display:

Fragment 0= one
Fragment 1= two
Fragment 2= three
Fragment 3= four

No comments:

Post a Comment