Visual Basic Strings
- Strings are enclosed in double quotes ( ” ” )
- Strings are a data type
- Strings are a collection of characters
Syntax
Dim stringName As String
Example
Here are several ways in which you can manipulate strings. Create a new VB Console Application and name it Strings. Next, type the following:
Sub Main()
Dim myName As String
myName = "thecodingguys"
Console.WriteLine(myName)
Console.ReadLine()
End Sub
Press F5 to debug and you should see the text thecodingguys. This is a simple string. Comment out the above code (CTRL+K+C).
Escape Character
In cases where you need to escape quotation marks which get in the way, see the following example:
Dim text As String
text = "Asim Said: "Hello World" "
Console.WriteLine(text)
Console.ReadLine()
If you run this (F5), you will get an error saying End of Statement Expected. The problem is that VB thinks that the line ends at “Asim Said:”, which is incorrect. So what we need to do is put four double quotes around Hello World, as such:
Dim text As String
text = "Asim Said: ""Hello World"" "
Console.WriteLine(text)
Console.ReadLine()
Output
New Line
When you need a new line, in C# the new line is simply \n; however in VB it is quite different. Comment out the above code and put this below:
Dim newLine As String
newLine = "This is a new" & vbCrLf & "Line"
Console.WriteLine(newLine)
Console.ReadLine()
This time we use the keyword vbCrLf to make a new line. Put your first line code in one quotation then your second in another, and in between put & vbCrLf &
Concatenation
Concatenation means joining two things together, in this case joining strings. It can be very useful when you want to link something together to form a sentence. You use the & ( or + ) to form a concatenation. For example:
Console.WriteLine(myName & " is awesome")
This will print out “thecodingguys is awesome”. myName is a variable we created earlier.
String.Format
The string.format method is useful for formatting your text, and you can format it in many ways. For example, say you wrote Console.WriteLine (“Your balance is £5″); however, if you gave this away to someone in the USA or Italy, they would see the British pound symbol instead of their own country’s currency symbol. This is where the .Net Framework becomes useful, as it can detect the user’s country / regional settings and format the piece of text to match those settings.
Example
Dim myNumber As Double = 158
strFormat = String.Format("{0:C}", myNumber)
Console.WriteLine(strFormat)
Console.ReadLine()
Now when you debug this (F5) you should see it print out 158 with your country’s currency symbol in front of it. For more ways to format strings, see the table below:
| Format | Description |
|---|---|
| {0:C} | Currency Symbol |
| {0:P} | Percentage Symbol |
| {0:N} | Thousand Separator |
The 0 in the format key specifies the parameter you want to format, followed by a colon and then the formatting type.
Example 2
This time we use two parameters.
Dim myNumber As Double = 158
Dim mySecondNumber As Integer = 25
strFormat = String.Format("{0:C} {1:P}", myNumber, mySecondNumber)
Console.WriteLine(strFormat)
Console.ReadLine()
In this example, the first parameter (myNumber) would appear as £158 and the second one would appear as 25%.
Manipulating Strings
There are many built-in methods available to you, so you can format strings in a number of ways. For example, you can format a string and make it all uppercase, you can remove spaces, or you can simply count the length of the string. In the next few examples, you will see a few useful methods used to manipulate strings.
In the following examples we will be working with a string called yourMessage which will have the text “Welcome to thecodingguys”, and another string finalResult which is set to nothing.
Dim yourMessage As String = "Welcome to thecodingguys" Dim finalResult As String = Nothing
For the sake of brevity we have omitted the Console.WritelIne(finalResult), from the examples; however if you want to add it, remember to add the Console.ReadLine() as well.
ToUpper and ToLower
The ToUpper and ToLower converts a string either to uppercase or lowercase format.
Example
finalResult = yourMessage.ToUpper() finalResult = yourMessage.ToLower()
Replace
The replace method replaces a string with another string. It takes two parameters, the old string and new string, and it can also take chars. In the following example, the spaces in yourMessage are removed and replaced with dashes.
finalResult = yourMessage.Replace(" ", "-")
Output
Substring
The substring method can be used to return parts of a string. For example, from the yourMessage string we can return the first 5 characters only. The substing method takes two parameters: the start index and length index as integer.
Example
finalResult = yourMessage.Substring(0, 7)
The 0 specifies the start point, in this case it is at the beginning; the 7 specifies how many characters we want from the starting point. To get all the characters you would do as follows:
finalResult = yourMessage.Substring(0, yourMessage.Length)
Output
Exceptions: Watch out for the ArguementOutofRangeException, which is common. This occurs when the string length is shorter than the points specified; for example if 0, 25 was used, it would give that exception as yourMessage is only 24 characters long.
Count
The count and length method return the length of the string.
finalResult = yourMessage.Count()
There are many more methods available.
| Method | Description | Output |
|---|---|---|
| Contains | See if a string contains a certain piece of text | Reutrns boolean (either true of false) |
| StartsWith | See if a string starts with a certain piece of text | Boolean – true of false |
| EndsWith | See if a string ends with a certain piece of text | Boolean – true of false. |
Summary
In this tutorial we showed you how to format and manipulate strings in Visual Basic.
- New Line is vbCrLF
- Escape Character “”
- String.Format followed by {0:C} or N or P. (The String.Format Full List is available at MSDN Library VB String Formats)
- Functions: ToUpper, ToLower, Replace, Substring, etc.
