Handling Strings in VB.net

By: Steven Holzner Emailed: 1768 times Printed: 2516 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.


VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
Creating a Windows Service in VB.net
What's New in VB .NET? A comparison of VB vs VB.net
Do Loop in VB.net
Your first VB.NET Crystal Reports - A step by step guide
The Option and Imports Statements in VB .NET
Arrays and Dynamic Arrays in VB.net
While Loop in VB.net
Stack data structure in VB.net
To read the entire worksheet in an Excel workbook through VB.net Code
Insert Pictures in Excel 2007 from VB.NET 2005
Multi Threaded Server Socket Programming in VB.net
How to create an XML file in VB.NET using Dataset
DataSet Class in VB.net
Creating a Windows Service Installer in VB.net
Send SMS using VB code
Most Emailed Articles (in VB.net)
File directory operationst in VB.net
Procedure Delegates in VB.net
Using Resume Next and Resume Line in VB.net
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
What is .NET Framework and the Common Language Runtime?
The Option and Imports Statements in VB .NET
Visual Basic Statements
Arrays and Dynamic Arrays in VB.net
Handling Dates and Times in VB.net
Queue data structure in VB.net
Vb.net text reader
Using Excel 2007 Chart in VB.NET 2005
Insert cell data in an Excel file using OLEDB in VB.net
Your first VB.NET Crystal Reports - A step by step guide