“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.
C# Programming Series
C# Arrays and Strings III
Yashavant Kanetkar
Strings
The string type is a predefined data type in C# and is an alias of the System.String class. Hence they can be used interchangeably. Instances of the string type represent Unicode character strings. The following statement shows how to assign a value to a string object:
string s = "Hello" ;
Here a reference s gets created on the stack while the object referred to by s gets created on the heap. C# supports two types of string variables, quoted and @-quoted. Quoted strings are normal strings that support escape sequences. If we do not want the compiler to process escape sequences we can use an @-quoted string. These strings are formed by prefixing the string with an '@' symbol. The main advantage of an @-quoted string is that, it simplifies the expression of path names. For example,
string s2 = @"C:\Articles\text.txt" ;
If @ symbol is not prefixed to this string then, its necessary to replace each '\' with '\\'. But the string shown below cannot be prefixed by the @ symbol if we want that the escape sequence should be processed.
string s1 = "hi \t there" ;
Whenever the compiler sees a '\t', it prints out a tab. Hence the output would become,
hi there
When we use an assignment operation between two reference variables, the reference to the first object gets copied into the other reference variable. But this is not the case with strings. In spite of being a reference type an assignment operation on two string variables results in creation of a new object. The value of the right-hand side string object gets copied into the new object and the reference to this new object gets assigned to the reference variable on the left-hand side of the assignment operator. This is shown in the following example.
using System;
namespace sample
{
class Class1
{
static void Main ( string[ ] args )
{
string s1 = "Good Morning" ;
string s2 ;
s2 = s1 ;
s1 = "Wake Up" ;
Console.WriteLine ( s1 ) ;
Console.WriteLine ( s2 ) ;
}
}
}The output of the program would be,
Wake up
Good Morning
In this program we have created two string references - s1 and s2. We have initialized s1 with a reference to an object containing "Good Morning" and kept s2 uninitialized. Then we have assigned s1 to s2. Normally both being references only the reference should have assigned to s1. But this does not happen. Instead a new object gets created that holds the same value contained in the object referred by s1.
Like System.Array, System.String class too defines various methods to perform routine operations on strings. The following programs illustrate use of the System.String methods.
using System ;
namespace sample
{
class Class1
{
static void Main ( string[ ] args )
{
string s1 = "kicit" ;
string s2 = "Nagpur" ;
Console.WriteLine ( "Char at 3rd position: " + s1 [ 2 ] ) ;
string s3 = string.Concat ( s1, s2 ) ;
Console.WriteLine ( s3 ) ;
Console.WriteLine ( "Length of s3: " + s3.Length ) ;
s3 = s3.Replace ( 'p', 'P' ) ;
Console.WriteLine ( s3 ) ;
s3 = string.Copy ( s2 ) ;
Console.WriteLine ( s3 ) ;
int c = s2.CompareTo ( s3 ) ;
if ( c < 0 )
Console.WriteLine ( "s2 is less than s3" ) ;
if ( c == 0 )
Console.WriteLine ( "s2 is equal to s3" ) ;
if ( c > 0 )
Console.WriteLine ( "s2 is greater than s3" ) ;
if ( s1 == s3 )
Console.WriteLine ( "s1 is equal to s3" ) ;
Else
Console.WriteLine ( "s1 is not equal to s3" ) ;
s3 = s1.ToUpper( ) ;
Console.WriteLine ( s3 ) ;
s3 = s2.Insert ( 6, "Mumbai" ) ;
Console.WriteLine ( s3 ) ;
s3 = s2.Remove ( 0, 1 ) ;
Console.WriteLine ( s3 ) ;
int fin = s1.IndexOf ( 'i' ) ;
Console.WriteLine ( "First index of i in s1: " + fin ) ;
int lin = s1.LastIndexOf ( 'i' ) ;
Console.WriteLine ( "Last index of i in s1: " + lin ) ;
string sub = s1.Substring ( fin, lin ) ;
Console.WriteLine ( "Substring: " + sub ) ;
int i = 10 ;
float f = 9.8f ;
s3 = string.Format ( "Value of i : {0} \nValue of f : {1}", i, f ) ;
Console.WriteLine ( s3 ) ;
}
}
}
In this program we have created two string variables s1 and s2 and initialized them to "kicit" and "Nagpur" respectively. The [ ] operator accesses individual characters of a string. Hence to access the 3rd character we have to write s1[ 2 ] which in the string s1 happens to be character 'c'.
The Concat( ) method is a static method which concatenates two strings and returns a new object which gets stored in s3. Hence the object referred to by s3 would contain "kicitNagpur". The Length property of a string returns the number of characters in a string. The length of s3 in our program is 11. The Replace( ) method replaces all the instances of a character ( mentioned as the first parameter) with the character given as the second parameter. Here we have replaced 'p' with a 'P' in string referred to by s3.
The Copy( ) method is a static method that copies all the characters from one string object to another. If the destination string is smaller in length, its length is increased automatically.
The CompareTo( ) method compares two strings alphabetically. This means a string with a starting alphabet 'a' will always be lesser than a string with starting alphabet as 'b'. This method returns a negative integer if the string object that has called the CompareTo( ) method contains a string which is lesser than the string contained in the object whose reference is passed as the method's parameter. It returns a positive number if the string objects are interchanged. It returns zero when both strings are equal.
In the same way the == operator compares two strings and returns a bool - true if the strings are equal and false if they are not.
The ToUpper( ) method creates a new object, stores in it the converted upper case string and returns the address of this object. This address is collected in s3. Note that s1 is not affected here. Hence we can draw a conclusion that the object which we use to call the methods to manipulates the string, does not affect the object.
The Insert( ) method inserts a string specified as the second parameter at the index specified as the first parameter. Here too s1 does not get affected and the returned string is collected in s3. So we get "NagpurMumbai" in s3.
In the Remove( ) method two arguments are passed. The first parameter is an index from where we wish to remove elements and the second parameter is the total number of elements to be removed. We have passed 0 and 1 meaning we wish to remove only the 0th element from s3. So the string s3 would now contain "agpur".
The IndexOf( ) method returns the index of first occurrence of a given character in the string. Similarly, LastIndexOf( ) method returns the index of last occurrence of a given character in the string. In our program the first index of 'i' happens to be 1 whereas the last index is 3.
The Substring( ) method returns a new string which starts at an index passed as the first parameter and ends at an index passed as the second parameter to the method.
The Format( ) method replaces the format specifiers (such as {0}, {1}, etc.) into string representations. This is a static method that returns the formatted string.
An important point to note is that the == operator is case insensitive. The Compare( ) and CompareTo( ) methods are two different methods in the string class. Compare( ) is a static method while CompareTo( ) is not. The Compare( ) method can be made case insensitive by passing true as the last parameter. Here's a code snippet that illustrates this.
string str1 = "hello" ; string str2 = "HELLO" ; bool chk1 = ( str1 == str2 ) ; //returns false int chk2 = ( String.Compare ( str1, str2, true ) ) ; // returns 0



