Thursday, January 12, 2012

String Functions in C#.net

          String Functions in C#.net 



1.Trim:

This function has 3 types 
1.Trim
2.TrimStart
3.TrimEnd

Trim function remove all white spaces.
ex: 
string s1="this is string type";
string s2=s1.Trim();
MessageBox.Show("-"+s2+"-");

2.TrimEnd():

This TrimEnd function remove certain characters which you are specified from end of the string.  
ex:
string s1="this is string type";
char[] schar={' ','g'};
string s2=s1.TrimEnd(schar);
MessageBox.Show("-"+s2+"-");

3.TrimStart

TrimStart is the same as TrimEnd function difference is starts from the starting position to the start of the string.

ex:
string myname1="sri rama ";
char[] mycararray={'','s'};
string myname2=myname1.TrimStart(mycararray);
MessageBox.Show("--"+myname2+"--");

4.String within a string

Finding string in a string,substring of a string by using index position of a char in the string.index position starts with 0

ex:
string string1="string with substring";
string substring="sub";
int indexpos=string1.IndexOf(substring);
MessageBox.Show("Substring at :"+indexpos);

5. string replace in string

Replacing string in a string.

Below is an example of replace a string within a string. It replaces the word Manipulatin with the correct spelling of Manipulation.
EX:
string string1="finding substring in a string";
string string2=string1.Replace("substring","substring found");
MessageBox.show("Substring found"+stri);

string MainString "String Manipulatin"; 
string CorrectString = MainString.Replace("Manipulatin", "Manipulation"); 
//SHOW CORRECT STRING 
MessageBox.Show("Correct string is : " + CorrectString);

6.Strip specified number of characters from string

This example show how you can strip a number of characters from a specified starting point within the string. The first number is the starting point in the string and the second is the amount of chrs to strip. 
string MainString = "S1111tring Manipulation"; 
string NewString = MainString.Remove(1,4); 

//SHOW OUTPUT 
MessageBox.Show(NewSring);

7.Split string with delimiter

The example below shows how to split the string into seperate parts via a specified dilemeter. The results get put into the Split array and called back via Split[0].
string MainString = "String Manipulation"; 
string [] Split = MainString.Split(new Char [] {' '}); 
//SHOW RESULT 
MessageBox.Show(Convert.ToString(Split[0])); 
MessageBox.Show(Convert.ToString(Split[1]));

8.Convert to title (proper) case

This is a simple extension method to convert a string to Proper Case or Title Case (ie the first character of each word is made upper case).
public static string ToTitleCase(this string title)
{
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;
    return textInfo.ToTitleCase(title);
}

No comments:

Post a Comment

amazon

Sukanya Samriddhi Account - SBI

SUKANYA SAMRIDDHI Account information by SBI SUKANYA SAMRIDDHI ACCOUNT : FACILITY AVAILABLE AT ALL BRANCHES OF SBI Sukanya Samriddhi ...