C# String Type: Manipulating Strings:
Comparing Strings:
The Compare method accepts two string parameters and returns the following int results:
. string 1 < string 2 = negative
. string 1 == string 2 = zero
. string 1 > string 2 = positive
An empty string,””, is always greater than null.
string str1="Mairaj";
string str2="Ahmed";
int Result = String.Compare(str1, str2);
Console.WriteLine("String.Compare({0}, {1}) = {2}\n",str1, str2, Result);
Some other methods for comparing strings are
· String.CompareOrdinal(str1,str2)
· Str1.CompareTo(Str2)
Checking for String Equality:
string str1="Mairaj";
string str2="Ahmed";
bool result=str1==str2;
Console.WriteLine("boolResult: {0}", result);
The result will be false because the two strings are not equal.
Another method for checking equality is
bool result = String.Equals(str1, str2);
Concatenating Strings:
string str1="Mairaj";
string str2="Ahmed";
string result=str1+" "+str2;
Console.WriteLine("Resulted string is " + result);
Here is output:
“Resulted string is Mairaj Ahmed”
Here a space is inserted between two strings and concatenated together.
Copying Strings:
· The Copy () method returns a copy of a string.
string str1="Mairaj";
string str2;
str2 = string.Copy(str1);
Console.WriteLine("str2 is " + str2);
Here is output:
“str2 is Mairaj”
· CopyTo () method copies specified characters into a character array:
string str1 = "Mairaj Ahmed Minhas";
char[] charArr = new char[str1.Length];
str1.CopyTo(7,charArr,0,5);
Console.WriteLine("{0}.CopyTo(0, charArr, 0, str1.Length) = ",str1);
foreach(char character in charArr)
{
Console.Write("{0} ", character);
}
Console.WriteLine("\n");
The output produced by this is
A h m e d
This example shows the CopyTo method filling a character array. It copies each character
from str1 into charArr, beginning at position 7 and continuing for next five characters.
· The Clone () method returns a copy of a string.
string str1 = "Mairaj Ahmed Minhas";
string result = (string)str1.Clone();
Console.WriteLine("string is "+ result);
Here is oiutput:
“String is Mairaj Ahmed Minhas”
Inspecting String Content:
· The StartsWith () method determines whether a string prefix matches a specified string.
string str1 = "Mairaj Ahmed Minhas";
bool result = str1.StartsWith("Ma");
Console.WriteLine("Result is {0} ", result);
The result will be true because str1 startswith “Ma”
· The EndsWith () method determines whether a string suffix matches a specified string.
string str1 = "Mairaj Ahmed Minhas";
bool result = str1.EndsWith("Ma");
Console.WriteLine("Result is {0} ", result);
The result will be false because str1 do not end with “Ma”
Extracting String Information:
· The IndexOf () method returns the position of a string. IndexOf () returns –1 if the string isn’t found.
string str1 = "Mairaj Ahmed Minhas";
int result = str1.IndexOf('A')
Console.WriteLine("Index of A is {0} ", result);
Here is output:
“Index of A is 7”
· The LastIndexOf () method returns the position of the last occurrence of a string or characters within a string.
string filePath = @"c:\Windows\Microsoft.NET\Framework";
int result = filePath.LastIndexOf(@".");
Console.WriteLine("Last index of . is {0} ", result);
Here is output:
“Last index of . is 20”
· Substring () methods returns a string extracted from a string value.
string str1 = "This is C# program";
string result = str1.Substring(5, 5);
Console.WriteLine("Substring is "+result);
Here is output:
“Substring is is on”
Trimming String Output:
· The Trim () method removes whitespace or a specified set of characters from the beginning
and ending of a string.
string str1 = " This is one line ";
string result = str1.Trim();
Console.WriteLine(result);
Here is output:
“This is one line”
· If you are concerned about trimming only one side of the string, you can use either
TrimEnd () or TrimStart ().
The TrimEnd () method removes a specified set of characters from the end of a string.
string str1 = " This is C# program ";
string result = str1.TrimEnd();
Console.WriteLine(result);
Here is output:
“ This is C# program”
· The TrimStart () method removes whitespace or a specified number of characters from the beginning of a string
string str1 = " This is C# program ";
string result = str1.TrimStart(new char[] {' '});
Console.WriteLine(result);
Here is output:
“This is C# program ”
Modifying String Content:
· The Insert () method returns a string where a specified string is placed in a specified position of an original string. All characters at and to the right of the insertion point are pushed right to make room for the inserted string.
string str1 = "This is C# program";
string result = str1.Insert(18, " one");
Console.WriteLine(result);
Here is output:
“This is C# program one”
· The Remove () method deletes a specified number of characters from a position in a string.
string str1 = "This is C# program";
string result = str1.Remove(0, 8);
Console.WriteLine(result);
Here is output:
“C# program”
· The Replace () method replaces all occurrences of a character or string with a new character or string, respectively.
string str1 = "This is C# program";
string result = str1.Replace('p', 'P');
Console.WriteLine(result);
Here is output:
“This is C# Program”
· The ToLower () method returns a copy of a string converted to lowercase characters.
string str1 = "This is C# program";
string result = str1.ToLower();
Console.WriteLine(result);
Here is output:
“this is c# program”
· The ToUpper () method returns a copy of a string converted to uppercase characters.
string str1 = "This is C# program";
string result = str1.ToUpper();
Console.WriteLine(result);
Here is output:
“THIS IS C# PROGRAM”
Splitting Strings:
· The split method () splits a string from a character passed to it.
string s = "there is a cat";
string[] words = s.Split(' ');//Split by space
foreach (string word in words)
{
Console.Write(word);
}
Here is output:
“thereisacat”
Comments
Post a Comment