Comparisons

String Comparison

Posted on Updated on

Comparisons
• Whenever the user read string or data from the command prompt it needs to be tested and compared for things like blank values, capital letters, extra spaces, incorrect formats and many things. The checking of the strings is known as String Comparison.
• Strings have many in-built methods and properties that can be used for string comparisons. Some are listed below:
Static methods of string
Compare Compares the values of two strings.
Copy Create a new string from an existing string
Concat Join two or more strings in to one string
Join Concatenates two or more strings or all elements in an array
equals Compares two strings to determine whether they contain the same value. returns true if the two values are equal; otherwise returns false

Methods and properties of each instance
Trim Removes copies of specified string from the beginning and end of the string
TrimEnd Removes copies of a specified string from the end of the string
TrimStart Removes copies of a specified from the beginning of the string
Char Returns a given character at a given location
Clone Returns a copy of the stored string
Contains Checks if a string contain certain characters
CompareTo Compares the value of first string with the second string and returns a negative number if first string is less than the second, a zero if equal and a positive number if the value of first string is greater
CopyTo Copies a portion or all of the string to a new string or character array
Length Returns the number of characters stored in the sting
IndexOf Returns the index (position) of the first match for a character or a string. Returns -1 in case the character is not found
LastIndexOf Returns the location of the last match for a character or sting. Returns -1 if the values is not found
StartsWith Determines whether the value stored in a string starts with a specified character or set of the characters. Returns true if there is a match and false if not
EndsWith Determines whether the value stored in a string ends with a specified character or set of the characters. Returns true if there is a match and false if not
Insert Inserts characters into a string at specified position
PadLeft Right justifies the values of a string and then pads any remaining spaces with a specified character
PadRight left justifies the values of a string and then pads any remaining spaces with a specified character
Spilt Breaks a string into sub strings using a specified breakpoint
Remove Deletes specified number of characters from specified position in a string
Substring Returns sub string from the original string starting at a specified location. The number of characters for the substring might also be specified but is not required
ToCharArray Copies the characters in the current string to a char array
ToLower Returns a copy of the current string in to all lower case letters
ToUpper Returns a copy of the current string in to upper case letters.

static void Main(string[] args)
{
string movie1 = ” rowdy rathore “;
string movie2 = “bol@bachan”;
Console.WriteLine(“{0}”,movie1 );
Console.WriteLine(“length of movie:{0}”, movie1.Length );
movie1 = movie1.Trim();
Console.WriteLine(“length of trimmed movie:{0}”, movie1.Length );

if (movie1.Contains(“rowdy”))
{
Console.WriteLine(“rowdy is contained in {0}”, movie1);
}
else
{
Console.WriteLine(“rowdy is not in {0}”, movie1);
}

int position = movie2.IndexOf(“@”);
Console.WriteLine(“position of @ in {0} is {1}”, movie2,position );

string movie1Insert = movie1.Insert(6, “bachan”);
Console.WriteLine(“after insertion {0}”,movie1Insert );

Console.WriteLine(“after PadLeft:{0}”,movie1 .PadLeft (20,’*’));
Console.WriteLine(“after PadLeft:{0}”, movie1.PadRight (20, ‘$’));

Console.WriteLine(“after removing:{0}”, movie1.Remove (5,3));

Console.WriteLine(“after replace:{0}”, movie1.Replace (“rowdy”,”strong”));

Console.WriteLine(“after sub string:{0}”, movie1.Substring (5,3));

Console.Read();
}
}

Some more example-
static void Main(string[] args)
{
string weekdays = “jan feb mar apr may june july aug”;
string[] arrweek = weekdays.Split(‘ ‘);
Console.WriteLine(“week days are:”);
foreach(string i in arrweek )
{
Console.WriteLine(“{0}”,i );
}
string showText = string.Join(“-“,arrweek );
Console.WriteLine(“joined text:{0}”,showText );
Console.Read();
}