Saturday 24 May 2014

Manipulating Strings

String manipulation is the most common part of the any programming language. Strings represent a sequence of characters. The easiest way to represent a sequence of characters in C# is by using a character array.
For example: 

                                           char [] charArray= new char[4];
                                           charArray [0]  =   'M';
                                           charArray [1]  =    'A';
                                           charArray [2]  =    'N';
                                           charArray [3]  =    'U';

 C# supports two types of strings, namely, immutable strings and mutable strings . C# also supports a feature known as regular expression that can be used for complex string manipulations and pattern matching.

We can create immutable strings using string or String objects in a number of ways:
  • Using ToString() method
  • Reading from the keyboard
  • Concatenating two objects
  • Copying from one object to another
  • Assigning string literals

Verbatim String :

Strings can also be created using what are known as verbatim strings. This type of strings start with @ symbol. This symbol tells the compiler that the string should be used verbatim even if it includes escape characters.

string S1= @ "\ABc\CSharpt\string";

String Methods:

string objects are immutable , meaning that we cannot modify the characters contained in them. How ever, since the string is an alias for the predefined System.String class in the CLR, there are many build in operations available that work with strings. All operations produce a modified version of the string rather than modifying the string on which the method is called.

Few Method are as below:


  1. Compare( )
  2. CompareTo ( )
  3. ConCat ( )
  4. Copy ( )
  5. CopyTo ( )
  6. Remove ( )
  7. Replace ( )
  8. Split ( )
  9. Trim ( )
  10. Substring ( )

INSERTING STRINGS

String methods are called using the string object on which we want to work. 

using System;
class StringFirstMethod
{
        public static void Main ()
         {
                  string s1="Hello";
                  string s2= s1.Insert(3,'M');                      //   s2    =  HelMlo

                   string s3= s2.Insert(5,'A');                    // s3    =   HelMlAo 
         }
}


COMPARING STRINGS

String class supports overload methods and operators to compare whether two strings are equal or not.
  • Overloaded Compare () method
  • Overloaded Equals () method
  • Overloaded == operator

COMPARE METHOD:


Compare method is further divided in two part.

(01)  First one take two string as parameters and compare them


int A= string.compare(s1,s2); 

This is case sensitive comparison and return different-different integer values which based the conditions.

  • if s1 equal to s2 then return Zero (0) integer
  • if s1 greater then s2 then return (1) positive integer
  • if s1 smaller then s2 then return (-1) negative integer

 Example:  if we have s1="abc" and s2="ABC", then we call the string.compare() method and store the value in integer variable

                int A= string.compare(s1,s2);

 This return -1, because smaller letter has a smaller ASCII value than uppercase letters.

 (02)  The second compare takes an additional bool type parameter to decide whether case should be ignored or not. If the bool parameter is true, case is igonored.

In this case equation is :

              int A= string.compare(s1,s2,true);

 This return 0, because we already pass true bool type parameter, hence it ignored the case. 
               
             abc = ABC
 


Equals Method
:

Here also we have two type of Equals method for comparing the strings.

           bool b1 = s2.Equals (s1);

           bool b2 = string.Equals (s2, s1);

These methods returns true if s1 and s2 are equal otherwise return false.

== Operator

We can say this is the traditional way of comparing two strings.

    bool b1= (s1 == s2) ;  
   returns true if both are equal otherwise false
 we can use this with if clause  also;

 if (s1==s2)
 {
   // conditional statement here
 }
 

2 comments:

stats counter
your visitor number