Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working With Strings in c#.

Author: Zunnair

In this article there are some simple and useful methods of c# which helps you to work with strings.

Comparing Strings


The Compare method compares two strings and returns an integer value. The return value of Compare method can be less than zero, greater than zero or equals to zero.

Value

Meaning

Less than zero

When first string is less than second.

Zero

When both strings are equal.

Greater than zero

When first string is greater than zero.


The following code compares two strings and return results on the System console.

Comparing two strings

Dim str1 As String = "ppp"
Dim str2 As String = "ccc"
Dim res As Int16 = String.Compare(str1, str2)
Console.WriteLine("First result:" + res.ToString())
str2 = "ttt"
res = String.Compare(str1, str2)
Console.WriteLine("Second result:" + res.ToString())
str1 = "ttt"
res = String.Compare(str1, str2)
Console.WriteLine("Third result:" + res.ToString())
'====================================================

The CompareTo method is an instance method. It compares a value (either a string or on object) with a string instance. Return values of this method are same as the Compare method. The following source code compares two strings.

' CompareTo Method
Dim str As String = "kkk"
Console.WriteLine(str.CompareTo(str1))

 

 

Copy and Concatenating Strings


The Concat method adds strings (or objects) and returns a new string. Using Concat method, you can add two strings, two objects and one string and one object or more combinations of these two.

The following source code concatenate two strings.

Dim str1 As String = "ppp"
Dim str2 As String = "ccc"
Dim strRes As String = String.Concat(str1, str2)
Console.WriteLine(strRes)

The following source code concatenates one string and one object.

Dim obj As Object = 12
strRes = String.Concat(str1, obj)
Console.WriteLine(strRes)

The Copy method copies contents of a string to another. The Copy method takes a string as input and returns another string with the same contents as the input string. For example, the following code copies str1 to strRes.

strRes = String.Copy(str1)
Console.WriteLine("Copy result :" + strRes)

The CopyTo method copies a specified number of characters from a specified position in this instance to a specified position in an array of characters. For example, the following example copies contents of str1 to an array of characters. You can also specify the starting character of a string and number of characters you want to copy to the array.

Dim
str1 As String = "pp"
Dim chrs(2) As Char
str1.CopyTo(0, chrs, 0, 2)
Console.WriteLine(chrs(0) + chrs(1))

The Clone method returns a new copy of a string in form of object. The following code creates a clone of str1.

Dim str1 As String = "ppp"
Dim objClone As Object = str1.Clone()
Console.WriteLine("Clone :" + objClone.ToString())

The Join method is useful when you need to insert a separator (String) between each element of a string array, yielding a single concatenated string. For example, the following sample inserts a comma and space (", ") between each element of an array of strings.

Dim str1 As String = "ppp"
Dim str2 As String = "ccc"
Dim str3 As String = "kkk"
Dim allStr() As String = New String() {str1, str2, str3}
Dim strRes As String = String.Join(", ", allStr)
Console.WriteLine("Join Results: " + strRes)

Adding, Removing and Replacing Strings


The Insert method inserts a specified string at a specified index position in an instance. For example, the following source code inserts "bbb" after second character in str1 and the result string is "pbbbpp".

Dim str1 As String = "ppp"
Dim strRes As String = str1.Insert(2, "bbb")
Console.WriteLine(strRes.ToString())

The Remove method deletes a specified number of characters from a specified position in a string. This method returns result as a string. For example, the following code removes three characters from index 3.

Dim s As String = "123abc000"
Console.WriteLine(s.Remove(3, 3))

The Replace method replaces all occurrences of a specified character in a string. For example, the following source code replaces all p character instances of str1 with character l and returns string "lll".

Dim str1 As String = "ppp"
Dim repStr As String = str1.Replace("p", "l")
Console.WriteLine("Replaced string:" + repStr.ToString())

The Split method separates strings by a specified set of characters and places these strings into an array of strings. For example, the following source code splits strArray based on ',' and stores all separated strings in an array.

Dim str1 As String = "ppp"
Dim str2 As String = "ccc"
Dim str3 As String = "kkk"
Dim strAll3 As String = str1 + ", " + str2 + ", " + str3
Dim strArray() As String = strAll3.Split(",")

Uppercase and Lowercase

The ToUpper and ToLower methods convert a string in uppercase and lowercase respectively. These methods are easy to use. The following code shows how to use ToUppler and ToLower methods.

Dim aStr As String = "adgas"
Dim bStr As String = "ABNMDWER"
Dim strRes As String = aStr.ToUpper()
Console.WriteLine("Uppercase:" + strRes.ToString())
strRes = bStr.ToLower()
Console.WriteLine("Lowercase:" + strRes.ToString())

Trimming and Removing Characters from Strings


The String class provides Trim, TrimStart and TrimEnd methods to trim strings. The Trim method removes white spaces from the beginning and end of a string. The TrimEnd method removes characters specified in an array of characters from the end of a string and TrimStart method removes characters specified in an array of characaters from the beginning of a string.

You can also use the Remove method to remove characters from a string. The Listing 2 code shows how to use these methods.

Dim str As String = " C# "
Console.WriteLine("Hello{0}World!", str)
Dim trStr As String = str.Trim()
Console.WriteLine("Hello{0}World!", trStr)

str = "Hello World!"
Dim chArr() As Char = {"e", "H", "l", "o", " "}
trStr = str.TrimStart(chArr)
Console.WriteLine(trStr)

str = "Hello World!"
Dim chArr1() As Char = {"e", "H", "l", "o", " "}
trStr = str.TrimEnd(chArr1)
Console.WriteLine(trStr)

Dim MyString As String = "Hello Delta World!"
Console.WriteLine(MyString.Remove(5, 10))

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to load Image in C# and set properties of the Picture Box

Disclaimer - Privacy
© 2002-2017 DevASP.net