You are on page 1of 3

String Manipulation

Len (string)

Returns the number of characters of the string

Left (string, length)

Returns the leftmost length number of characters of the string

Right (string, length)

Returns the rightmost length number of characters of the string

Mid (string, start [, length])

Returns length number of characters of the string beginning at position start. If length is omitted,
the function returns all characters from the start position through the end of the string.

EXAMPLES:

Assume variable strProgName contains Visual Basic

Function
Left (January, 3)

Resulting String
Jan

Right (January, 2)

ry

Mid (January, 2, 1)

Mid (January, 4, 2)

ua

Left (strProgName, 6)

Visual

Right (strProgName, 5)

Basic

Mid (strProgName, 8, 1)

Mid (strProgName, 8)

Basic

To search a string to determine if it contains another string, use the Instr function.
Instr (start, string1, string2 [, compare])

start is the numeric expression that sets the starting position for the search,
string1 is the string being searched
string2 is the string expression you want to find.
Compare is either 0 or 1. 0 means case-sensitive, the default setting.

Examples:
Function
Instr ( 1, Have a nice day, nice, 0)

Result (starting position of string2 within string1)


8

Instr ( 1, Have a nice day, NICE, 0)

Instr ( 1, Have a nice day, NICE, 1)

Instr (9, This is an Instr function example, instr)

Instr (9, This is an Instr function example, instr, 1)

12

Other string functions and code are shown below.


Asc(character)

converts one character into ASCII code number

Chr(character code)

converts character code into a character

Replace(String1, String2,String3,[more stuff]

Returns a string value.


String1 is the string to search
String2 is the string you want to find
String3 is the string you want to replace for string2 in string1.

StrReverse Function
Requirements
Version 2
Returns a string in which the character order of a specified string is reversed.
StrReverse(string1)

The string1 argument is the string whose characters are to be reversed. If string1 is a zero-length string (""), a
zero-length string is returned. If string1 is Null, an error occurs.
Remarks
The following example uses the StrReverse function to return a string in reverse order:
Dim MyStr
MyStr = StrReverse("VBScript")

' MyStr contains

Code from VB sample program StringFunctions


Option Explicit
Dim CharacterCode, NumChars, SingleCharacter, StartWhere As String
Dim SearchString As String
Dim CharPosition, SearchPosition As Byte
Private Sub cmdASCII_Click()
SingleCharacter = InputBox("Which character do you want to convert to a code number?", "ASCII")
lblASCII.Caption = SingleCharacter & " is " & Asc(SingleCharacter)
End Sub
Private Sub cmdCharacter_Click()
CharacterCode = InputBox("Which character code do you want to convert to a character?", "Character From Code #")
lblCharacter.Caption = CharacterCode & " is " & Chr(Val(CharacterCode))
End Sub
Private Sub cmdExit_Click()
End
End Sub
Private Sub cmdInstr_Click()
SearchString = InputBox("Enter the character or string of characters you are looking for", "Search String")
SearchPosition = InStr(1, txtString.Text, SearchString)
If SearchPosition = 0 Then
lblInstr.Caption = "String not found"
Else
lblInstr.Caption = SearchString & " begins at position " & SearchPosition
End If
End Sub
Private Sub cmdLeft_Click()
NumChars = InputBox("How many characters do you want to copy?", "Left$")

lblLeft.Caption = Left$(txtString.Text, Val(NumChars))


End Sub
Private Sub cmdLength_Click()
lblLength.Caption = Len(txtString.Text)
End Sub
Private Sub cmdMiddle_Click()
StartWhere = InputBox("Where do you want to start to copy (enter position number)?", "Start for Mid$")
NumChars = InputBox("How many characters do you want to copy?", "Number of Characters for Mid$")
lblMiddle.Caption = Mid$(txtString.Text, Val(StartWhere), Val(NumChars))
End Sub
Private Sub cmdRight_Click()
NumChars = InputBox("How many characters do you want to copy?", "Right$")
lblRight.Caption = Right$(txtString.Text, Val(NumChars))
End Sub
Private Sub cmdStretch_Click()
lblStretch.Caption = ""
For CharPosition = 1 To Len(txtString.Text)
lblStretch.Caption = lblStretch.Caption & Mid$(txtString.Text, CharPosition, 1) & " "
Next CharPosition
End Sub

You might also like