Tuesday, August 23, 2016

Common String functions and manipulations in C#

In this article I will explain some commonly used String functions
in C#. Even though there are many, here I will explain only the most
commonly used ones.

(1) string.IsNullOrEmpty() :

This function specifies whether the value of the string is null or empty.

For eg.

string str = "Praise the Lord";

string.IsNullOrEmpty(str);

Will return false in this case.

(2) Length :

This will return the number of characters in the string
For eg.
string str = "Praise the Lord";
int i=str.Length;

Here value of "i" will be 15.


(3) Contains :

This function determines whether the string contains the specified element.
For eg.
string str = "Praise the Lord";
bool val = str.Contains('j'); // characters can be specified inside single quotes

bool val2=str.Contains("the");  // string can be specified inside double quotes

Here value of  val will be false and that of val2 will be true.

(4) EndsWith :

This function determines whether the string ends with the specified string element.
For eg.
string str = "Praise the Lord";
bool val=str.EndsWith("Lord");

Here the value of val will be true.

(5) Equals :

This function determines whether the string is equal to the specified string element.
For eg.
string str = "Praise the Lord";
string str2="abc";
bool val=str.Equals(str2);

Here the value of val will be false.

(6) IndexOf :

This function returns the integer value that represents the index of the first occurrence 
of the specified string or character element.
For eg.
string str = "Praise the Lord";
int val = str.IndexOf('t');  // characters can be specified inside single quotes
int val2 = str.IndexOf("the"); // string can be specified inside double quotes

Here the values of val and val2 will be 7.

(7) Insert :

This will insert the specified string at the specified index position of the string.
For eg.
string str = "Praise the Lord";
string val=str.Insert(15, " Jesus");

Here the value of val will be "Praise the Lord Jesus".

(8) LastIndexOf :

This will return an integer value of the index of the last occurrence of the specified
element in the string.
For eg.
string str = "Praise the Lord";
int val = str.LastIndexOf('e');
int val2 = str.LastIndexOf("the");


Here the values of val  will be 9 and that of val2 will be 7.

(9) Remove :

This function deletes the characters from the string. If the parameter for length 
is not specified it will delete all the characters starting from the index specified.
If length is specified it will delete that number of characters from the index specified.
For eg.
string str = "abcdefg";
string val = str.Remove(3);
string val2 = str.Remove(3,2);

Here the output of val will be "abc" and that of val2 will be "abcfg".

(10) Replace :

This function replaces all the occurrences of the specified string or character with the
new value.
For eg.
string str = "abcdefg";
string val = str2.Replace("abc","*");
string val2 = str2.Replace('a', '#');

In this case output of val is "*defg" and that of val2 is "#bcdefg"

(11) Split :

This function returns a string array that contains the substrings of the string
delimited by the character element specified.
For eg.
string str = "abc,def,ghi";
string[] arr = str.Split(',');

Here it will return an array with elements abc,def and ghi.

(12) StartsWith :

This function determines whether the string starts with the specified string value.
For eg.
string str = "Praise the Lord";
bool val=str.StartsWith("Praise");

Here the value of val will be true.

(13) Substring :

This function retrieves the substring starting from the specified index of the string.
If length is specified, it will retrieve substring of the specified length from the
specified index of the string.
For eg.
string str = "Praise the Lord";
string sub1 = str.Substring(11);
string sub2 = str.Substring(11,2);

Here value of sub1 is "Lord" and that of sub2 is "Lo".

(14) ToLower :

This function returns a copy of the string after converting to Lower Case.
For eg.
string str = "Praise the Lord";
string str2 = str.ToLower();

Here value of str2 will be "praise the lord".

(14) ToUpper :

This function returns a copy of the string after converting to UpperCase.
For eg.
string str = "Praise the Lord";
string str2 = str.ToUpper();

Here value of str2 will be "PRAISE THE LORD".

(15) Trim :

This function removes all leading and trailing white space characters from the string
For eg.
string str = " ab c ";
string str2 = str.Trim();

Here output of str2 will be "ab c".

(16) TrimStart :

This function removes all leading white space characters from the string.
It can also be used for removing all leading occurrences of a set of characters 
specified in a character array element.

For eg.
string str = " ab c ";
string str2 = str.TrimStart();

Here output of str2 will be "ab c ".

(17) TrimEnd :

This function removes all trailing white space characters from the string.
It can also be used for removing all trailing occurrences of a set of characters 
specified in a character array element.

For eg.
string str = " ab c ";
string str2 = str.TrimEnd();

Here output of str2 will be " ab c".


No comments:

Post a Comment