Programming Tutorials

Handling Strings in VB.net

By: Steven Holzner in VB.net Tutorials on 2008-11-25  

You've decided to lead the way into the future by letting your users type in sentences as commands to your program. Unfortunately, this means that you have to parse (i.e., break down to individual words) what they type. So what was that string function that lets you break a string into smaller strings again? We'll get an overview of string handling in this topic. Strings are supported by the .NET String class in Visual Basic. You declare a string this way:

Dim strText As String

As with other types of variables, you can also initialize a string when you declare it, like this:

Dim myString As String = "Welcome to Visual Basic"

A string can contain up to approximately 2 billion Unicode characters, and it can grow or shrink to match the data you place in it. There are quite a number of string-handling functions built into Visual Basic.NET. For example, you use Left, Mid, and Right to divide a string into substrings, you find the length of a string with Len, and so on.

Besides the string-handling functions that are built into VB .NET, many .NET framework functions are built into the String class that VB .NET uses. For example, the Visual Basic UCase function will convert strings to upper case, and so will the String class's ToUpper method. That means that I can convert a text string to uppercase either by using the VB .NET UCase function, or the String class's ToUpper method:

Option Strict On
Module Module1
    Sub Main()
        Dim strText1 As String = "welcome to visual basic"
        Dim strText2 As String
        Dim strText3 As String
        strText2 = UCase(strText1)
        strText3 = strText1.ToUpper
        System.Console.WriteLine(strText2)
        System.Console.WriteLine(strText3)
    End Sub

End Module

Here's another example-I can use the Mid function to get a substring from the middle of another string if I pass it that string, the location to start extracting the substring from (starting a position 1), and the length of the substring. I can do the same thing with the String class's Substring method, if I pass it the location to start extracting the substring from (starting a position 0 this time), and the length of the substring. In this case, I'll extract "look" from "Hey, look here!":

Module Module1
    Sub Main()
        Dim strText1 As String = "Hey, look here!"
        Dim strText2 As String
        Dim strText3 As String
        strText2 = Mid(strText1, 6, 4)
        strText3 = strText1.Substring(5, 4)
        System.Console.WriteLine(strText2)
        System.Console.WriteLine(strText3)
    End Sub
End Module

Here's what you see when you execute this example:

look
look
Press any key to continue

For reference, the popular Visual Basic string-handling functions and methods appear in Table 2.7, organized by task (new in VB .NET: note that you now cannot use LSet and RSet to assign one data type to another). Note in particular the string-trimming functions, which are very handy and can trim leading or trailing spaces or other characters.

Table 2.7: String-handling functions and methods.

To do this

Use this

Concatenate two strings

&, +, String.Concat, String.Join

Compare two strings

StrComp, String.Compare, String.Equals, String.CompareTo

Convert strings

StrConv, CStr, String. ToString

Copying strings

=, String.Copy

Convert to lowercase or uppercase

Format, Lcase, Ucase, String.Format, String. ToUpper, String. ToLower

Convert to and from numbers

Str, Val.Format, String.Format

Create string of a repeating character

Space, String, String.String

Create an array of strings from one string

String.Split

Find length of a string

Len, String.Length

Format a string

Format, String.Format

Get a substring

Mid, String.SubString

Insert a substring

String.Insert

Justify a string with padding

LSet, Rset, String.PadLeft, String.PadRight

Manipulate strings

InStr, Left, LTrim, Mid, Right, RTrim, Trim, String.Trim, String.TrimEnd, String.TrimStart

Remove text

Mid, String.Remove

Replace text

Mid, String.Replace

Set string comparison rules

Option Compare

Search strings

InStr, String.Chars, String.IndexOf, String.IndexOfAny, String.LastIndexOf, String.LastIndexOf Any

Trim leading or trailing spaces

LTrim, RTrim, Trim, String.Trim, String.TrimEnd, String.TrimStart

Work with character codes

Asc, AscW, Chr

Here's another point you should know-to concatenate (join) strings together, you can use the & or + operators, or the String class's Concat method. Here's an example breaking up a long string over several lines:

Dim Msg As String
Msg = "Well, there is a problem " _
& "with your program. I am not sure " _
& "what the problem is, but there is " _
& "definitely something wrong."

Fixed-Length Strings

VB6 and earlier supported fixed-length strings, where you can specify a non-changing length for a string, but that's changed in VB .NET to match the .NET Framework. However, there is a special class in VB .NET-VB6.FixedLengthString,-that supports fixed-length strings; for example, this declaration in VB6, which declares a string of 1000 characters:

Dim strString1 As String * 1000

now becomes:

Dim strString1 As New VB6.FixedLengthString(1000)
Tip 

If you're going to use fixed-length strings in structures (that is, user-defined types), you should know that the fixed-length string is not automatically created when the structure is created. You must initialize the fixed-length string before referencing the structure in code.

Tip 

You also can create strings of spaces with the SPC function, or insert tabs into strings with the TAB function.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)