# String Class

**URL:** <https://forums.thehomestack.io/t/string-class/123>\
**Category:** vb.Net\
**Created:** [January 10, 2004, 5:23am UTC](https://forums.thehomestack.io/t/string-class/123 "2004-01-10T05:23:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forums.thehomestack.io/uploads/default/original/1X/161426793bc0d8bdf0d1a6692b3e44446861cc72.jpeg) [@system](https://forums.thehomestack.io/u/system)\
**Post date:** [January 10, 2004, 5:23am UTC](https://forums.thehomestack.io/t/string-class/123/1 "2004-01-10T05:23:17Z")

</div>

A F e w U s e f u l S t r i n g C l a s s M e t h o d s

By Michael McIntyre

[mikemc@getdotnetcode.com](mailto:mikemc@getdotnetcode.com)

All strings are derived from the .Net Framework String Class which provides many string manipulation methods. To see all the String Class methods search .Net help using this search string: String Members

Here are some examples of the more useful methods of the String Class in action.

IndexOf(string) - Reports the index of the first occurrence of a string within a string.

Dim string1 As String = “abcd”

’ Report the index of ‘b’ in string1

Console.WriteLine(string1.IndexOf(“b”))

RESULT: 1

NOTE: Result is 1 because arrays are 0 based. In the string ‘a’ is at index 0, ‘b’ at index 1 and so forth.

Insert(index, string) - Inserts a string at a specified index position in another string.

Dim string1 As String = “abcd”

Console.WriteLine(string1)

RESULT: abcd

’ Insert a dash into string1 at the index of ‘b’

’ Assign the result to string1

string1 = string1.Insert(string1.IndexOf(“b”), “-”)

Console.WriteLine(string1)

RESULT: a-bcd

Replace(string, string) - Replaces all occurrences of a specified string within a string, with another string.

Dim string1 As String = “The contest winner is NameGoesHere. Congratulations NameGoesHere!”

Console.WriteLine(string1)

RESULT: The contest winner is NameGoesHere. Congratulations NameGoesHere!

’ Replace all occurances of ‘NameGoesHere’ in string1

’ with ‘Mr. Jones’.

’ Assign the result to string1

string1 = string1.Replace(“NameGoesHere”, “Mr. Jones”)

Console.WriteLine(string1)

RESULT: The contest winner is Mr. Jones. Congratulations Mr. Jones!

Remove(startIndex, count) - Deletes a specified number of characters from a string beginning at a specified position.

Dim string1 As String = “Bread Butter Eggs”

Console.WriteLine(string1)

RESULT: Bread Butter Eggs

’ Remove 7 characters starting at the index of ‘Butter’

string1 = string1.Remove(string1.IndexOf(“Butter”), 7)

Console.WriteLine(string1)

RESULT: Bread Eggs

Split(char) - Identifies the substrings in a string that are delimited by one or more characters, then places the substrings into a string array.

Join - Concatenates a separator string between each element of a string array, yielding a single concatenated string.

Dim string1 As String = “System.Windows.Forms.Button”

Console.WriteLine(string1)

RESULT: System.Windows.Forms.Button

’ Split string1 into a string array named stringArray1

Dim stringArray1() As String = string1.Split(“.”)

’ Write each element of stringArray1 to a line in the console.

Dim i = 0

For i = 0 To stringArray1.GetUpperBound(0)

Console.WriteLine(stringArray1(i))

Next

RESULT:

System

Windows

Forms

Button

’ Create a new single string from stringArray1 concatenating

’ a separator string between each element joined from stringArray1.

Dim newString As String = String.Join(“-”, stringArray1)

Console.WriteLine(newString)

RESULT: System-Windows-Forms-Button

ToCharArray - Copies the characters in a string to a Unicode character array.

Dim string1 As String = “abcd”

Console.WriteLine(string1)

RESULT: abcd

’ Split string1 into a char array named charArray1

Dim charArray1() As Char = string1.ToCharArray()

’ Write each element of charArray1 to a line in the console.

Dim i = 0

For i = 0 To charArray1.GetUpperBound(0)

Console.WriteLine(charArray1(i))

Next

RESULT:

a

b

c

d

’ Create a new single string from the charArray1

Dim newString As String

For i = 0 To charArray1.GetUpperBound(0)

’ Concatenate newString, a char from charArray1,

’ and a space.

newString &= charArray1(i) & " "

Next

Console.WriteLine(newString)

RESULT: a b c d

Format - Replaces each format specification in a specified string with the textual equivalent of a corresponding object’s value.

Dim airMiles As Integer = 1145

’ Create a format string which includes the format specifier

’ {0:#,###}

Dim formatString As String = “Air miles balance: {0:#,###}”

Console.WriteLine(formatString)

RESULT: Air miles balance: {0:#,###}

’ Use the format string to format airMiles.

’ Assign the result to newString.

Dim newString As String = Format(formatString, airMiles)

Console.WriteLine(newString)

RESULT: Air miles balance: 1,145
