Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Strings in VB - Part I (http://www.go4expert.com/articles/strings-vb-part-i-t2047/)

Andrew 29Nov2006 09:08

Strings in VB - Part I
 

Substrings



Left, Right and Mid are three Visual Basic functions to locate sections of a string. All of the following examples use the string "strExample" with the following value.

strExample = "Please try to substring this"

The Left function returns the requested number of characters from the left side of the string.

Code: VB

Me.Print Left(strExample, 10)

Returns, "Please try"

The Right function returns the requested number of characters from the right side of the string.

Code: VB

Me.Print Right(strExample, 18)

Returns, "do what you can do"

The Mid function returns the middle of a string. It takes three parameters, the string, the starting position and the length of the string to be returned.

Code: VB

Me.Print Mid(strExample, 8, 6)

Returns, "try to"

The Len function is used to determine the length of a string. The next example uses the Len and the Mid function to print a string of text down the form.
Code: VB

Dim counter As Integer

Const strExample As String = "Please try to do what you can do"
 
For counter = 1 To Len(strExample)
    Me.Print Mid(strExample, counter, 1)
Next counter

String Concatenation



Strings are joined together using the & operator.
Code: VB

Dim strFirst As String, strSecond As String, strResult As String
strFirst = "Too "
strSecond = "bad!"
strResult = strFirst & strSecond
Me.Print strResult

The concatenation operator is useful for building up long strings a bit at a time.
Code: VB

Dim strSQL As String

strSQL = "SELECT Surname, TelNo " & _
"FROM Customer " & _
"ORDER BY Surname"

Searching a String



InStr and InStrRev are two Visual Basic functions to locate one string inside another. InStr locates the first occurrence of the string, and InStrRev locates the last occurrence.

Code: VB

Const strExample As String = "Please try to do what you can do"
Dim pos As Integer
pos = InStr(strExample, "do")
If pos > 0 Then
    Me.Print "Found first occurrence of string at position " & pos
Else
    Me.Print "String not found"
End If

Returns, "Found first occurrence of string at position 15"
Code: VB

Const strExample As String = "Please try to do what you can do"
Dim pos As Integer
pos = InStrRev(strExample, "do")
If pos > 0 Then
    Me.Print "Found last occurrence of string at position " & pos
Else
    Me.Print "String not found"
End If

Returns, "Found last occurrence of string at position 31"

Steel9561 1May2008 03:07

Re: Strings in VB - Part I
 
I think that the Mid function is often used whenever you need to extract a certain string from a big string. I often do it in the programs I do. You extract only what you want and then you work with that string or display it to the user in a listbox, combo box, etc..

Luis Lazo

articlechamp 21Apr2009 15:58

Re: Strings in VB - Part I
 
thnks its wnderful :)


All times are GMT +5.5. The time now is 14:43.