3.1 String Class
The most importent built-in class is the string class. The string class and the
StringBuilder class provide ways to perform string manipulations. The string
class provides an immutable object
string s = "Hello";
which means that once the value of the string instance is set, it cannot be changed.
Even though it appears that the application is changing the value of the string
instance, it is actually returning a new instance of the string class in memory.
// mystring.cs
using System;
class mystring
{
public static void Main()
{
string s1 = "otto";
int length = s1.Length;
Console.WriteLine(s1); // => otto
Console.WriteLine(length); // => 4
string s2 = "willi";
s2 = s2.Replace(’l’,’t’);
Console.WriteLine(s2); // witti
string s3 = "Johannesburg";
string result = s3.Substring(3,5);
Console.WriteLine(result); // => annes
43
44 CHAPTER 3. STRING AND STRINGBUILDER
string s4 = "olli&ulli&ruedi";
string[] textarray = s4.Split(’&’);
Console.WriteLine(textarray[1]); // => ulli
string s5 = string.Join(":",textarray);
Console.WriteLine(s5); // => olli:ulli:ruedi
char[] s6 = { ’o’, ’p’, ’a’ };
string s7 = new string(s6);
Console.WriteLine(s7); // => opa
string s8 = "xeNa";
string s9 = s8.ToUpper();
Console.WriteLine(s9); // => XENA
string s10 = "WILLI HANS";
string s11 = s10.ToLower();
Console.WriteLine("s11 = " + s11); // => willi hans
// use + to concatenate strings
string s12 = "Carl-";
string s13 = "Otto";
string s14 = s12 + s13;
Console.WriteLine("s14 = " + s14);
// use Equals() to compare strings
// case sensitive
bool b1 = s12.Equals(s13);
Console.WriteLine("b1 = " + b1); // => False
// can also use overloaded == to compare strings
// case sensitive
bool b2 = (s12 == s13);
Console.WriteLine("b2 = " + b2); // => False
// copy a string
string s15 = string.Copy(s14);
Console.WriteLine("s15 = " + s15);
} // end Main
}
Arrays of strings can be implemented as follows.
// stringarrays.cs
3.2. CONVERT CLASS 45
using System;
class stringarrays
{
public static void Main()
{
// one-dimensional array of strings
string[] keywords = new string[] { "as", "do", "if", "in" };
Console.WriteLine(keywords[3]); // => in
// one-dimensional array of strings
string[] names = { "willi", "ola", "xena" };
Console.WriteLine(names[2]); // => xena
// two-dimensional array of strings
string[,] strArry = {{"1","one"}, {"2","two"}, {"3","three"}};
Console.WriteLine(strArry[0,0]); // => 1
Console.WriteLine(strArry[2,0]); // => 3
} // end Main
}
3.2 Convert Class
Using the Convert class we can convert string to numbers (integers and floating
point) and numbers (integers and floating point) to strings. The methods are
string ToString(T x) // any numerical type
bool ToBoolean(string s)
byte ToByte(string s)
char ToChar(string s)
short ToInt16(string s)
int ToInt32(string s)
long ToInt64(string s)
float ToSingle(string s)
double ToDouble(string s)
decimal ToDecimal(string s)
An example is
// MyConvert.cs
using System;
public class MyConvert
46 CHAPTER 3. STRING AND STRINGBUILDER
{
public static void Main()
{
int i = 34;
string s1 = Convert.ToString(i);
Console.WriteLine("s1 = " + s1);
double x = 3.14159;
string s2 = Convert.ToString(x);
Console.WriteLine("s2 = " + s2);
bool b = Convert.ToBoolean("true");
Console.WriteLine("b = " + b);
char c = Convert.ToChar("x");
Console.WriteLine("c = " + c);
int j = Convert.ToInt32("12345");
Console.WriteLine("j = " + j);
string s3 = "3.14159";
double y = Convert.ToDouble(s3);
Console.WriteLine("y = " + y);
}
}
3.3 StringBuilder Class
The StringBuilder class represents a mutable string a characters. It is called mutable
because it can be modified once it has been created by using the methods
Append(), Insert(), Remove() and Replace(). The StringBuilder class is defined
in the System.Text namespace. Thus we have to add the following line in our
application.
using System.Text;
Using the StringBuilder class.
// mystringbuilder.cs
using System;
using System.Text; // for StringBuilder
class mystringbuilder
{
public static void Main()
{
string s = "carl";
StringBuilder b1 = new StringBuilder(s.Length+12);
3.3. STRINGBUILDER CLASS 47
b1.Append("carl");
b1.Append("-uli");
Console.WriteLine(b1); // => carl-uli
b1.Remove(3,2);
Console.WriteLine(b1); // => caruli
StringBuilder b2 = new StringBuilder("A.C");
b2.Insert(2,"B.");
Console.WriteLine(b2); // => A.B.C
b2.Replace(’.’,’:’);
Console.WriteLine(b2); // => A:B:C
StringBuilder b3 = new StringBuilder("stringbuilder");
b3.Remove(4,9);
Console.WriteLine(b3); // => stri
}
}
Another application of the StringBuilder class is given by generating the Thue-
Morse sequence. We apply recursion, i.e. the method mythuemorse calls itself.
// thuemorse.cs
using System;
using System.Text;
class ThueMorse
{
public static void Main()
{
for(int i=0;i<7;i++)
Console.WriteLine(mythuemorse(i));
}
public static StringBuilder mythuemorse(int n)
{
if(n==0) return new StringBuilder("0",50);
StringBuilder tm = mythuemorse(n-1);
StringBuilder tm2 = new StringBuilder("",30);
for(int i=0;i
if(tm[i] == ’0’) tm2.Append("01"); else tm2.Append("10");
return tm2;
}
}
No comments:
Post a Comment