Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 119,387 times

Contents

Related Categories

String Functions - Getting text

Getting text

To get all the text from a string you use the following syntax:

VariableOrProperty = String

For example, the following code sets Form1 caption property to the string contained in String1

' Declare the variable
Dim sString As String
' Fill the variable
sString = "Hello World"
' Assign String1 to Form1's Caption
Form1.Caption = sString

However, sometimes you will find you only want to get part of a string. The most basic functions are Left, Right and Mid. Left gets a specified number of letters from the left side of a string. Left uses the following syntax:

Left$(String, Length)

The following code will display a message box with 'Hel'. It has taken three letters from the left side of String1

' Declare the variable
Dim sString As String
' Fill the variable
sString = "Hello"
' Get the first three characters from String1
MsgBox Left$(sString, 3)

Right uses the same syntax except returns the specified number of letters from the right side of a string. The following code will display a message box with 'llo'. It has taken three letters from the right side of String1

' Declare the variable
Dim sString As String
' Fill the variable
sString = "Hello"
' Get the first three characters from String1
MsgBox Right$(sString, 3)

Mid gets a specified number of letters from a specified point in a string (counted from the left). Mid uses the following syntax:

Mid$(String, Start, Length)

The following code will display a message box with 'll'. It has taken two letters from the third character onwards from String1

' Declare the variable
Dim String1 As String
' Fill the variable
String1 = "Hello"
' Get two characters from pos 3 onwards in String1
MsgBox Mid$(String1, 3, 2)

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [69] String Functions

    Posted by KöllMorgan on 14 Jan 2007

    Here we go again:


    After hours of debugging, I found out the only instance the first word on the second line does not get picked up is when I physically tab or hit enter and enter or type ad...

  • Re: [69] String Functions

    Posted by KöllMorgan on 14 Jan 2007

    Replying to my own post...Hiya!


    I found out the trick to grabbing the first word in the string using the above code:


    InStr(1," " & YourString & " ", " " & YourSearc...

  • Re: [69] String Functions

    Posted by KöllMorgan on 13 Jan 2007

    Hello!


     


    Wonderful, helpful site, by the way...


     


    I am working on a small program for a friend and noticed the below code.  Works flawlessly, accept t...

  • Posted by koti_sastry on 06 Dec 2005

    i know i am answering to an oldest question left unanswered..

    but.. i just wanted to answer it..

    just for some one who is searching on a specific topic ...and end up in finding only question...

  • Another way of removing excess spaces

    Posted by fatwalrus on 17 Feb 2005

    [b]I included a code extract here but you have to click on "read comments" to see it all.[/b]

    [i]Basically it just searches the text string for 2 spaces in row, and replaces it with one space, the...