VBNet String Processing

by Linda Quinn

Close Window

Contains

System.String.Contains

Tells you if a string Contains another string:
Dim str1 As String = "Lions, tigers and bears"
Dim str2 As String = "tigers"
Dim str3 As String = "Elephants"
Dim hasIt as Boolean






In this example, the value of hasIt will be TRUE:.
hasIt = str1.Contains(str2)

In this example, the value of hasIt will be FALSE:
hasIt = str1.Contains(str3)


Concat

System.String.Concat

Concatenate up to four strings:
NewString = String.Concat (str1, str2)
NewString = String.Concat (str1, str2, str3, str4)



You can concatenate two, three or four strings.

You can also concatenate the strings in a string array.

NewString = String.Concat (strArray)


Empty

System.String.Empty

This function returns an empty string.
An empty string is a zero-length string, or "".


xStr = String.Empty

xstr is now "".


IndexOf

System.String.IndexOf

Find the position (or IndexOf), a character or string object in a string.

Dim str1 As String = "Lions, tigers and bears"
Dim str2 As string = "tigers"
Dim str3 As string = "bears"

Dim x As Integer = str1.IndexOf(str2)





The value of x will be 8.

Dim y As Integer = str1.IndexOf(str3)
The value of y will be 19.

If the searched for string is not found, the value returned is -1.
If the string being searched is empty, the value returned is 0.



LastIndexOf

System.String.LastIndexOf

Finds the last position (or IndexOf), a character or string object in a string.
This is similar to the right function in VB6.
The last position of a string will be the rightmost position.


Dim str1 As String = "No bananas, no bananas today."
Dim str2 As string = "bananas"
Dim x As Integer = str1.LastIndexOf(str2)




The value of x will be 16. This is the rightmost incidence of banana.

If the searched for string is not found, the value returned is -1.
If the string being searched is empty, the value returned is 0.



Insert

System.String.Insert(start index, value)
Inserts a string within another string at a specified starting point.

Dim str1 As String = "Lions tigers and bears"
Dim str2 As string = "and "
Dim str3 As string = "Oh My!"
Dim NewString as String = ""

NewString = str1.Insert(7, str2)






NewString will contain the string "Lions and tigers and bears"

NewString = str1.Insert(28,str3)
NewString will contain the string "Lions and tigers and bears Oh My!"


Join

System.String.Join(separator, stringArray)
System.String.Join(separator, stringArray, startIndex, count)


Creates a single string from a string array.
Join also sends a string containing the Separator value for the new string.


Dim strArr() As String = {"Boston", "Chicago", "Detroit", "Memphis", "Denver"}
Dim sep As String = "|"
Dim newString As String

newString = String.Join(sep, strArr)







newString will contain "Boston|Chicago|Detroit|Memphis|Denver"

Join can also specify a starting index in the array and a count of elements to extract.

newString = String.Join(sep, strArr, 2, 3)


This example will join the array starting at the second element and will extract three array elements.
The result will be "Chicago|Detroit|Memphis"



Length

System.String.Length

Find the number of characters in a string.

Dim str1 As String = "Today is Wednesday"
Dim x As Integer = str1.Length



The value of x will be 18.


Trim / TrimStart / TrimEnd

System.String.Trim
System.String.TrimStart
System.String.TrimEnd

Trim removes whitespace from the beginning and end of a string.
TrimStart removes whitespace from the beginning of a string.
TrimEnd removes whitespace from the end of a string.


Dim str1 As String = "    Abe Lincoln    "
Dim NewString As String = str1.Trim


NewString will be "Abe Lincoln".

Dim str1 As String = "    Abe Lincoln    "
Dim NewString As String = str1.TrimStart


NewString will be "Abe Lincoln    ".

Dim str1 As String = "    Abe Lincoln    "
Dim NewString As String = str1.TrimEnd


NewString will be "   Abe Lincoln".



Split

System.String.Split

Takes a string and breaks it into a string array, based on the specified separator character.

Dim str1 As String = "Boston, Chicago, Detroit, Memphis, Denver"
Dim strArray() As String = str1.Split(",")



This example will create an array (strArray) with these elements:
strArray(0) = "Boston"
strArray(1) = "Chicago"
strArray(2) = "Detroit"
strArray(3) = "Memphis"
strArray(4) = "Denver"






The array was created by splitting the string into elements where the specified separator is found.
In this case, the designated separator character was a comma.



Replace

System.String.Replace

Replace a set of characters or a string, from another string.

Dim str1 As String = "Today is Wednesday"
Dim oldValue As String = "Wednesday"
Dim newValue As String = "Thursday"

NewString = str1.Replace(oldValue, newValue)






The value of NewString is "Today is Thursday".


StartsWith

System.String.StartsWith(string)

Determine if a string Starts With a specified character or string.

Dim str1 As String = "<h1> Heading </h1>"
Dim markup as Boolean
Dim value As String = "<"

markup = str1.StartsWith(value)






The value of markup will be TRUE in this example.


Char

System.String.Char (index)

Get the character at the index location in the string. This uses a zero-based index.

Dim str1 As String = "Happy New Year!"
Dim val As Char

val = str1(0)




The value of val will be H.

val = str1(4)


The value of val will be y.

val = str1(10)


The value of val will be Y.


PadLeft/PadRight

System.String.PadLeft(width)
System.String.PadLeft(width, character)
System.String.PadRight(width)
System.String.PadRight(width, character)


PadLeft right-aligns the characters in a string and pads the leftside to the specified length.

PadRight left-aligns the characters in a string and pads the rightside to the specific length.

Both PadLeft and PadRight have an optional parameter to specify the character to fill the string.

If this is not specified, the padding will be blanks.

Dim str1 As String = "Happy Birthday"
NewString = str1.PadRight(20)


The value of NewString is "Happy Birthday      ".

Dim str1 As String = "Happy Birthday"
NewString = str1.PadRight(5)


The value of NewString is "Happy Birthday"


In this example, the period character is specified.
This will create leading for a Table Of Contents.


Dim str1 As String = "Chapter 1"
NewString = str1.PadRight(15, ".")


The valueNewstring is "......Chapter.1"






=================================================================
This content was created by Linda Quinn of LQNet.

See http://www.lqnet.com for a great collection of articles on this and other topics.

=================================================================
Copyright © 2006-2008, LQ Systems,Inc. All rights reserved.


====================================================================
Want an expert to help with your project?    LQ Systems, Inc.   Business Solutions
====================================================================