rString
String is set of characters and you can assign only once and u cant change after u assign the string and we can see some string operations below
Left of String:
This function gives the left part of string
string str;
str = "Iam a memeber of dotnet spider";
//left of string
Console.WriteLine(left(str, 10));
public static String left(String s, int len)
{
return s.Substring(0, Math.Min(len, s.Length));
}
Right of Stirng:
This function gives the right of string
string str;
str = "Iam a memeber of dotnet spider";
//Right of string
Console.WriteLine(right(str, 10));
public static string right(string s, int count)
{
string newString = String.Empty;
if (s != null && count > 0)
{
int startIndex = s.Length - count;
if (startIndex > 0)
newString = s.Substring(startIndex, count);
else
newString = s;
}
return newString;
}
Search with in the string:
This function will search for a specific string with in the main string
//String search
string searchin = "Iam a memeber of dotnet spider";
string searchFor = "dotnet";
int firstCharacter = searchin.IndexOf(searchFor);
Console.WriteLine("First occurrence: {0}", firstCharacter);
if (firstCharacter == -1)
{
Console.WriteLine("dotnet string doesnot exsits in the main string");
}
if (firstCharacter >= 0)
{
Console.WriteLine("dotnet string exsits in the main string");
}
Remove character in string:
This function removes some characters from string by specifing the distance to remove from which character to which character
////Remove character in string
string str1 = "Dotnetspider Memeber";
Console.WriteLine(str1.Remove(7, 5));
Joining the strings:
This function joins two strings to one string
///joining the strings
string MyString = "Hello";
string MyString2 = "World";
string JoinedString = MyString + MyString2;
Console.WriteLine(JoinedString);
Extracting substring:
This function extracts sub string from main string
////Extracting substring
string MyClasses = "Math 101 - Algebra";
string MySubstring = MyClasses.Substring(6);
Console.WriteLine(MySubstring);
Spliting strings:
This function splits the string into array of words
//// spliting strings
string MString = "The quick brown fox ran around!";
string[] MyStringSplit = new string[6];
MyStringSplit = MString.Split(new char[] { ' ' }, 6);
Console.WriteLine(MyStringSplit[1] + MyStringSplit[2]);
Trimming String Spaces:
This function trims the spaces in string
/// Trimming String Spaces
string MyString1 = " Hello, World ! ";
MyString1.TrimStart();
Console.WriteLine(MyString1);
MyString1.TrimEnd();
Console.WriteLine(MyString1);
MyString1.Trim(char.Parse("!"));
Console.WriteLine(MyString1);
string arrays:
This is the array of strings
///string arrays
string[] str2 = { "This", "is", "a", "test." };
Console.WriteLine("Original array: ");
for (int i = 0; i < str2.Length; i++)
Console.Write(str2[i] + " ");
Console.WriteLine("\n");
Construct a substring:
This function get some part of strirg
// construct a substring
string orgstr = "C# makes strings easy.";
string substr = orgstr.Substring(5, 12);
Console.WriteLine("\n");
Console.WriteLine("orgstr: " + orgstr);
Console.WriteLine("substr: " + substr);
Compare two strings:
This function Compare two strings based on case sensitive and and ignoring case sensitive
/// Compare two strings
string s1 = "abcd";
string s2 = "ABCD";
int result;
result = string.Compare(s1, s2);
Console.WriteLine("compare s1: {0}, s2: {1}, result: {2}\n", s1, s2, result);
// overloaded compare, takes boolean "ignore case"
//(true = ignore case)
result = string.Compare(s1, s2, true);
Console.WriteLine("Compare insensitive. result: {0}\n",
result);
String Copy:
This function copys string from one to other string
///string copy
string s4 = "ABCD";
// the string copy method
string s5 = string.Copy(s4);
Console.WriteLine(
"s5 copied from s4: {0}", s5);
// use the overloaded operator
string s6 = s5;
Console.WriteLine("s6 = s5: {0}", s6);
Split and join the string into parts:
This function splits string into parts
// Split the string into parts.
string str4 = "One if by land, two if by sea.";
char[] seps = { ' ', '.', ',' };
string[] parts = str4.Split(seps);
Console.WriteLine("Pieces from split: ");
for (int i = 0; i < parts.Length; i++)
Console.WriteLine(parts[i]);
This function joins the splited parts
// Now, join the parts.
string whole = String.Join(" | ", parts);
Console.WriteLine("Result of join: ");
Console.WriteLine(whole);
Insert string in another string:
This function insertf stirng into another string
///Insert
string stri = "This is sample";
Console.WriteLine("Original string: " + stri);
stri = stri.Insert(5, "is a ");
Console.WriteLine(stri);
Replace string with other string:
// Replace string
string stri = "This is sample";
Console.WriteLine("Original string: " + stri);
stri = stri.Replace("is", "was");
Console.WriteLine(stri);
Replace Character within the string:
This function replaces a character with in the string with the other character
// Replace characters
string stri = "This is sample";
Console.WriteLine("Original string: " + stri);
stri = stri.Replace('a', 'X');
Console.WriteLine(stri);
Remove some characters from string:
This Fucntion removes some characters with in the string
// Remove
stri = stri.Remove(4, 5);
Console.WriteLine(stri);