X



Home>Tutorial>

String Concatenation and Manipulation in Visual Basic

Nuelson Penuel Monday, September 7, 2020 Visual Basic.Net

Summary

In this post, am going to show you a simple way of string concatenation or manipulation (left function, right function and mid function e.t.c) in Visual Basic. I have tried as much as possible to make it brief and easy to understood by even a beginner.

String Manipulation

In Visual Basic, string manipulation is very important as it helps to process data that come in the form of non-numeric types such as author names, addresses, gender, cities e.t.c In Visual Basic, strings can be manipulated in various forms such as manipulating string from Lower case to UpperCase vise versa.

What is String Concatenation

When we mentioned string concatenation, we are referring to a way of combining two or more strings to form a single string. For instance var1 = "Hello", var2 = "welcome to Emmason Community". Now to combine or concatenate this string, we need to use & or + symbols as follows; varResult = var1 & var2 and the varResult = "Hello welcome to Emmason Community"

Example of String Concatenation

Create a new console application in Visual Basic.Net and name it StrManipulation.

Module StrManipulation
Sub Main()
Dim str1 As String= "Hi there!"
Dim str2 As String = "Welcome to Emmason Community"
Dim finalStr As String=str1 & str2
Console.WriteLine(finalStr)
Console.ReadLine
End Sub
End Module

String Concatenation Using the Built-in Functions

A function is used to accept a certain input and return a value which is passed on to the main program to finish the execution.There are numerous string manipulation functions built into Visual Basic such as Left(), Right(), Len(), Mid, StrReverse() e.t.c.

Visual Basic Len Function

The Len function when used returns an integer value which is equivalent to length of the given string. The syntax is;

Len (string)

Visual Basic Len Function

Let's try a real example as below

Module StrLenFunction
Sub Main()
Dim str1 As String= "Hi there!"
Dim str2 As String = "Welcome to Emmason Community"
Dim finalStr As String=str1 & str2
Console.WriteLine(Len(finalStr))
Console.ReadLine
End Sub
End Module
And the output would be 37

Visual Basic Right Function

The Right function extracts a specific length of characters from a string starting from the right side. The syntax is;

Right(string, length)

Visual Basic Right Function Example


Module StrRightFunction
Sub Main()
Dim str1 As String= "Hi there!"
Dim str2 As String = "Welcome to Emmason Community"
Dim finalStr As String=str1 & str2
Console.WriteLine(Right(finalStr, 9))
Console.ReadLine
End Sub
End Module
The above program returns 9 characters equivalent to "community" from the finalStr

Visual Basic Left Function

Unlike the Right function, the Left function extracts a specific length of characters from a string starting from the left side. The syntax is;

Left(String, Length)

Visual Basic Left Function Example


Module StrLeftFunction
Sub Main()
Dim str1 As String= "Hi there!"
Dim str2 As String = "Welcome to Emmason Community"
Dim finalStr As String=str1 & str2
Console.WriteLine(Left(finalStr, 9))
Console.ReadLine
End Sub
End Module
The result will be "Hi there!"

Visual Basic Mid Function

Unlike the Right function and the Left function, the Mid function is used to extract a specific length of characters from a middle of a string. However, you must specify the starting index and and the length of characters. The syntax is;

Mid(String, Start, Length)

Visual Basic Mid Function Example


Module StrMidFunction
Sub Main()
Dim str1 As String= "Hi there!"
Dim str2 As String = "Welcome to Emmason Community"
Dim finalStr As String=str1 & str2
Console.WriteLine(Mid(finalStr, 3,5))
Console.ReadLine
End Sub
End Module
The result will be "there!"

| | Views(393)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services