In this article, I have compiled some common String operations that we encounter while working with the String class. All the samples are based on two pre-declared string variables: strOriginal and strModified.
C#
string strOriginal = "These functions will come handy";
string strModified = String.Empty;
Dim strOriginal As String = "These functions will come handy"
Dim strModified As String = String.Empty
1. Iterate a String – You can use the ‘for’ loop or ‘foreach’ loop to iterate through a string. The ‘for’ loop gives you more flexibility over the iteration.
C#
for (int i = 0; i < strOriginal.Length; i++)
{
MessageBox.Show(strOriginal[i].ToString());
}
or
foreach (char c in strOriginal)
{
MessageBox.Show(c.ToString());
}
For i As Integer = 0 To strOriginal.Length - 1
MessageBox.Show(strOriginal(i).ToString())
Next i
Or
For Each c As Char In strOriginal
MessageBox.Show(c.ToString())
Next c
2. Split a String – You can split strings using String.Split(). The method takes an array of chars, representing characters to be used as delimiters. In this example, we will be splitting the strOriginal string using ‘space’ as delimiter.
C#
char[] delim = {' '};
string[] strArr = strOriginal.Split(delim);
foreach (string s in strArr)
{
MessageBox.Show(s);
}
Dim delim As Char() = {" "c}
Dim strArr As String() = strOriginal.Split(delim)
For Each s As String In strArr
MessageBox.Show(s)
Next s
3. Extract SubStrings from a String – The String.Substring() retrieves a substring from a string starting from a specified character position. You can also specify the length.
C#
// only starting position specified
strModified = strOriginal.Substring(25);
MessageBox.Show(strModified);
// starting position and length of string to be extracted specified
strModified = strOriginal.Substring(20, 3);
MessageBox.Show(strModified);
' only starting position specified
strModified = strOriginal.Substring(25)
MessageBox.Show(strModified)
' starting position and length of string to be extracted specified
strModified = strOriginal.Substring(20, 3)
MessageBox.Show(strModified)
4. Create a String array – There are different ways to create a Single Dimensional and Multi Dimensional String arrays. Let us explore some of them:
C#
// Single Dimensional String Array
string[] strArr = new string[3] { "string 1", "string 2", "string 3"};
// Omit Size of Array
string[] strArr1 = new string[] { "string 1", "string 2", "string 3" };
// Omit new keyword
string[] strArr2 = {"string 1", "string 2", "string 3"};
// Multi Dimensional String Array
string[,] strArr3 = new string[2, 2] { { "string 1", "string 2" }, { "string 3", "string 4" } };
// Omit Size of Array
string[,] strArr4 = new string[,] { { "string 1", "string 2" }, { "string 3", "string 4" } };
// Omit new keyword
string[,] strArr5 = { { "string 1", "string 2" }, { "string 3", "string 4" } };
' Single Dimensional String Array
Dim strArr As String() = New String(2) { "string 1", "string 2", "string 3"}
' Omit Size of Array
Dim strArr1 As String() = New String() { "string 1", "string 2", "string 3" }
' Omit new keyword
Dim strArr2 As String() = {"string 1", "string 2", "string 3"}
' Multi Dimensional String Array
Dim strArr3 As String(,) = New String(1, 1) { { "string 1", "string 2" }, { "string 3", "string 4" } }
' Omit Size of Array
Dim strArr4 As String(,) = New String(, ) { { "string 1", "string 2" }, { "string 3", "string 4" } }
' Omit new keyword
Dim strArr5 As String(,) = { { "string 1", "string 2" }, { "string 3", "string 4" } }
5. Reverse a String – One of the simplest ways to reverse a string is to use the StrReverse() function. To use it in C#, you need to add a reference to the Microsoft.VisualBasic dll.
C#
string strModified = Microsoft.VisualBasic.Strings.StrReverse(strOriginal);
MessageBox.Show(strModified);
Dim strModified As String = StrReverse(strOriginal)
MsgBox(strModified)
6. Compare Two Strings – You can use the String.Compare() to compare two strings. The third parameter is a Boolean parameter that determines if the search is case sensitive(false) or not(true).
C#
if ((string.Compare(strOriginal, strModified, false)) < 0)
{
MessageBox.Show("strOriginal is less than strOriginal1");
}
else if ((string.Compare(strOriginal, strModified, false)) > 0)
{
MessageBox.Show("strOriginal is more than strOriginal1");
}
else if ((string.Compare(strOriginal, strModified, false)) == 0)
{
MessageBox.Show("Both strings are equal");
}
If (String.Compare(strOriginal, strModified, False)) < 0 Then
MessageBox.Show("strOriginal is less than strOriginal1")
ElseIf (String.Compare(strOriginal, strModified, False)) > 0 Then
MessageBox.Show("strOriginal is more than strOriginal1")
ElseIf (String.Compare(strOriginal, strModified, False)) = 0 Then
MessageBox.Show("Both strings are equal")
End If
7. Convert a String to Byte[] (Byte Array) – The Encoding.GetBytes() encodes all the characters into a sequence of bytes. The method contains six overloads out of which we will be using the Encoding.GetBytes(String).
C#
byte[] b = Encoding.Unicode.GetBytes(strOriginal);
Dim b As Byte() = Encoding.Unicode.GetBytes(strOriginal)
Note: You can adopt different character encoding schemes (ASCII, Unicode etc.) based on your requirement.
8. Convert Byte[] to String – The Encoding.GetString() decodes a sequence of bytes into a string.
C#
// Assuming you have a Byte Array byte[] b
strModified = Encoding.Unicode.GetString(b);
' Assuming you have a Byte Array byte[] b
strModified = Encoding.Unicode.GetString(b)
ppppline
0 Please Share a Your Opinion.: