Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Focus On Visual Basic
A special Focus Section of Visual Basic Explorer

Part 3

<< Prev] [Next >>

Simple string functions

What follows is a brief description of some of the simpler, more common string handling functions that Visual Basic provides.

Len(string)

Determines the length of string.
sName = "Basic"
i = Len(sName)  'Returns the value 5.

Chr$(character_code)

Creates a one-character string from character_code.
s = Chr$(66)  'Creates "B"
As demonstrated above, the character code for "B" is 66.

Asc(string)

Returns the character code of the first character in string.
i = Asc("Basic")  'Returns the value 66.

Space$(length)

Creates a string of spaces length characters long.
sBuffer = Space$(10)  'Returns "          "

String$(length, string)

Creates a string length characters long that contains only the first character of string.  If string is longer than one character, only the first character of string is used.
s = String$(10, "ABCD")  'Returns "AAAAAAAAAA"

LCase$(string)

Converts all alphabetic characters in string to lower case.
s = LCase$("A1B2c3d4")  'Returns "a1b2c3d4"

UCase$(string)

Converts all alphabetic characters in string to upper case.
s = UCase$("A1B2c3d4")  'Returns "A1B2C3D4"

LTrim$(string)

Removes spaces from the left side of a string.
s = LTrim$("  ABC  ")  'Returns "ABC  "

RTrim$(string)

Removes spaces from the right side of string.
s = RTrim$("  ABC  ")  'Returns "  ABC"

Trim$(string)

Removes spaces from both sides of string.
s = Trim$("  ABC  ")  'Returns "ABC"

StrReverse(string)

NOTE:  This function is new to Visual Basic, version 6.0.

Reverses the order of all the characters in string.

s = StrReverse("ABCD")  'Returns "DCBA"

Substrings

Substrings are pieces of a string.  What follows are a few string functions that help you deal with substrings.

Left$(string, length)

Returns length characters from the left side of string.
s = Left$("This is a test", 4)  'Returns "This"

Right$(string, length)

Returns length characters from the right side of string.
s = Right$("This is a test", 4)  'Returns "test"

Mid$(string, start[, length])

Mid$ is unique in that it is a command as well as a function.  As a function, it returns a substring from string, beginning at the start character position for length characters.  As a command, it replaces a substring of string, beginning at the start character position for length characters.  If length is not provided, all the characters from start to the end of the string are included in the substring.
s = Mid$("This is a test", 6, 4)  'Returns "is a"
s = Mid$("This is a test", 6)     'Returns "is a test"

sMid = "This is a test"
Mid$(sMid, 6, 4) = "1234567890"  'Only 4 chars will be used
'sMid now contains "This 1234 test"

sMid = "This is a test"
Mid$(sMid, 6) = "1234567890"
'sMid now contains "This 123456789"

sMid = "This is a test"
Mid$(sMid, 6) = "12"
'sMid now contains "This 12 a test"

Split(string[, delimiter[, count[, compare_mode]]])

NOTE:  This function is new to Visual Basic, version 6.0.

Splits string and returns the substrings as an array of strings.  Delimiter identifies where the split(s) should occur.  If delimiter is not provided, a space (" ") is used.  Count is the maximum number of substrings to return.  If count is not provided, all substrings are to be returned.  Compare_mode indicates how the search for delimiter inside string is to be performed.  Refer to the section Comparing Strings for more information about compare_mode.

Dim i As Integer
Dim sArray As Variant

sArray = Split("This is a test")
For i = LBound(sArray) To UBound(sArray)
  Debug.Print i; sArray(i)
Next

'This will display the following:
'  0 This
'  1 is
'  2 a
'  3 test

Notice that the return array is zero-based (not one-based).  A few more examples follow:
sArray = Split("This is a test", "s")
'Returns:
'  "Thi"
'  " i"
'  " a te"
'  "t"

sArray = Split("This is a test", , 2)
'This splits the array like the first example,
'  but only 2 substrings will be returned:
'  "This"
'  "is a test"

sArray = Split("34,658,-39", ",")
'Returns:
'  "34"
'  "658"
'  "-39"

Join(string_array[, delimiter])

NOTE:  This function is new to Visual Basic, version 6.0.

Concatenates the substrings in the array string_array into a single string.  Delimiter is a string that will be concatenated between each of the substrings.  If delimiter is not provided, a space will be used.  This is the opposite of the Split function.

Dim sArray(4 to 7) As String
Dim sJoin As String

sArray(4) = "This"
sArray(5) = "is"
sArray(6) = "a"
sArray(7) = "test"
sJoin = Join(sArray, " ")   'Returns "This is a test"
sJoin = Join(sArray)        'Returns "This is a test"
sJoin = Join(sArray, ",")   'Returns "This,is,a,test"
sJoin = Join(sArray, "..")  'Returns "This..is..a..test"
sJoin = Join(sArray, "")    'Returns "Thisisatest"

<< Prev] [Next >>





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.