Click here to check my latest exciting videos on youtube
Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment

Locations of visitors to this page

Latest Articles

Move to top
Commonly used string Functions
Posted By Sarin on Mar 27, 2012     RSS Feeds     Latest Hinduism news

Commonly used string Functions:
Check if string is null or empty
This is the most commonly used string operation. This is the function to test a string for either Null reference or Emptiness.  
Ex: Whenever we retrieve data from the database, then for columns storing null values, this function should be used
Before VS 2008, the same thing was done without this function as
   string testStr = null;
if (testStr == string.Empty || testStr == null)
"String 'testStr' is empty or null<br/>");
This can be written now as
   testStr =
if (String.IsNullOrEmpty(testStr))
"String 'testStr' is empty or null");
String 'testStr' is empty or null
String 'testStr' is empty or null
Trim unwanted characters
The trim function has three variations Trim, TrimStart and TrimEnd. The first example show how to use the Trim(). It strips all white spaces from both the start and end of the string. This is quite usually used while inserting data into the database or while retrieving data from the database. 

string sMantra = " Hare Krishna ";
string sNewMantra = sMantra.Trim();
Hare Krishna

Sometimes, the user enters spaces at the beginning or at the end while giving their input, trim function is very useful in such cases.
We can also specify an array of character which should be trimmed from eithr the start of the string or the end of the string
sNewMantra = "Trim with character specified :" + sMantra.Trim(new Char [] 'H',' ');
Trim with character specified: are Krishna

TrimEnd is a variation to trim function where you can strip characters only at the end of the string, below example first strips the space then the n so the output is String Manipulation.  
sNewMantra = "TrimEnd :"+ sMantra.TrimEnd();

 function is commonly used in file read operation where you trim the character at the end of the string

TrimEnd :' Hare Krishna'
TrimStart is the same as TrimEnd function. Instead of trimming the character at the end of the string, it does it to the start of the string.  

    sNewMantra = "TrimStart :"+ sMantra.TrimStart();
TrimStart :'Hare Krishna '

Find String within string
This code shows how to search for a string within a string and either returns an index position of the searched string or -1 if the string is not found.  We can also specify whether we want to do a case sensitive search or case insensitive search
sMantra.IndexOf(SearchString)); //Case sensitive
StringComparison.OrdinalIgnoreCase))  //Case insensitive
if there are multiple occurrences of the search string, then we can look for the last occurrence using  
lastindexof function

sMantra.LastIndexOf(SearchString, StringComparison.OrdinalIgnoreCase)
  Replace part of a string with another string  
Below is an example on how to replace a substring within a string.  
Above code will replace Krishna with rama in input string ‘smantra’

Strip specified number of characters from string  
This example shows how you can strip a number of characters from a specified starting point within the string. There are two overloaded methods: First method will take a single input parameter and this method will remove all the character starting from the index position specified in this input parameter.
Second method will take two parameter, first parameter is the starting point in the string and the second is the amount of chars to strip from the starting point.


PadLeft and PadRight
These two functions pad a string to the left or right till the specified length. By default the character is a space but an overload lets you specify an alternative char.  
     sNewMantra = sMantra.Trim().PadRight(20, 'a');
"Padding ten Characters: '" + sNewMantra + "'");
Padding ten Characters: 'Hare Krishnaaaaaaaaa'

Till the length of string is equal to20, char ‘a’ is appended
Similarly, pad left can be used as  

  sNewMantra = sMantra.Trim().PadLeft(20, 'a');
"Padding ten Characters left: '" + sNewMantra + "'");
Padding ten Characters left: 'aaaaaaaaHare Krishna'

StartsWith and EndsWith
Both methods return true or false if a string starts or ends with the specified string.  

if (sMantra.TrimStart().StartsWith("hare"))
"String Starts With Hare.");
if (sMantra.TrimEnd().EndsWith("na"))
"String ends With na.");
First if statement checks if string starts with ‘hare’ and second if statement checks if the string ends with ‘na’
String Starts With Hare.
String ends With na.
Concatenation Of string
String concatenation is the act of combining two strings by adding the contents of one string to the end of another. This can also be done using the concatenation operator (+). however concat method format looks better. We used the following example:
sNewMantra = sMantra + "Hare Rama";
Concat method allows many strings to be passed as arguments. The strings are joined together and a new concatenated string is returned:
  sNewMantra = string.Concat(sMantra, "Hare Krishna"," Krishna Kirshna"," hare hare");
 Hare Krishna Hare Rama
  Hare Krishna Hare Krishna Krishna Kirshna hare hare

Inserting Strings into Strings
The String class also provides a method that inserts one string into the middle of another. The Insert method acts upon a string variable or literal and uses two parameters. The first is an integer that indicates the position where the second string is to be inserted. This integer counts characters from the left with zero indicating that the insertion will be at the beginning of the string. The second parameter is the string to be inserted.
sNewMantra = sMantra.Insert(6, "Hare");
String Insert method: ' Hare HareKrishna

Check if a particular string exist
This is a simple Boolean check to see if one string is contained within another.  Output will be true if it exists else the output will be false.
   if (sMantra.Contains("hare"))
"String containg word 'Hare'");

String contains word 'Hare'
Copy a String into another string  

A simple way to copy a string to another is to use the String.Copy(). It works similar to assigning a string to another using the ‘=’ operator.

    sNewMantra = String.Copy(sMantra);

This is commonly used when you want to perform an operation on the string. At the same time, you want to have a reference to the old string as well.  
Hare Krishna  
Format a String  
The String.Format() enables the string’s content to be determined dynamically at runtime. It accepts placeholders in braces \ whose content is replaced dynamically at runtime as shown below:
  sNewMantra = String.Format("{0}{1}", sMantra," Hare Rama");
Hare Krishna Hare Rama


Share this to your friends. One of your friend is waiting for your share.
Share on Google+ Share on Tumblr Print Browser Favorite
Related Articles
Ways of generating random password or string
Create property class of database table
Different string functions in SQL-Part 2
Call Codebehind method from GridView ItemTemplate Eval function
Advanced Strings Format for numbers
Commonly used string Functions
Advanced Datetime Format for string
Commonly used string operation in SQL
Different Strings Format for numbers
Parse comma separated input string in SQL

Post Comment