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.
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
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures 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
Throwing an Exception in VB.net
Comments