Comparing 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.
The following code compares two strings and return results on the System console.
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.
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 concatenates one string and one object.
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.
The Clone method returns a new copy of a string in form of object. The following code creates a clone of str1.
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.
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".
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.
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".
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.
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.
Formatting Strings.
You can use the Format method to create formatted strings and concatenate multiple strings representing multiple
objects. The Format method automatically converts any passed object into a string.
For example, the following code uses integer, floating number and string values and format them into a string using
the Format method.
Figure 1.
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.
Padding Strings.
The PadLeft and PadRight methods can be used to pad strings. The PadLeft method right-aligns and pads a string so
that its rightmost character is the specified distance from the beginning of the string. The PadRight method left-
aligns and pads a string so that its rightmost character is a specified distance from the end of the string. These
methods return new String objects that can either be padded with empty spaces or with custom characters. Listign 3
shows how to use these methods.
Figure 2.
The StringBuilder class represents a mutable string of characters. It's called mutable because it can be modified once
it has been created by using Append, Insert, Remove, and Replace methods.
Note: When there are multiple concatenation operations are involved with strings, for performance reasons it is
recommended to use StringBuilder instead of String.
The StringBuilder class is defined in the System.Text namespace. Before you use the StringBuilder class make sure
you add the following line in your application:
imports System.Text
Property Description
Represents the maximum number of characters that can be contained in the
Capacity memory allocated by the current instance.
Chars Represens the character at the specified position.
Length Represents the number of characters.
MaxCapacit Returns the maximum capacity.
y
Method Description
Append Appends a string at the end of this string.
AppendFormat Appends a formatted string.
EnsureCapaciry Ensures that the capacity of string is as specified value.
Inserts Inserts string at the specified position.
Remove Removes a range of characters from the string.
Replace Replaces all occurrences of a character from the string.
Using StringBuilder properties and methods is pretty simple. Listing 4 uses StringBuilder
class to append, insert, remove and replace characters of a string.
Listing 4. Using StringBuilder class to append, add, replace and remove characters.