String
|
Tips >> Visual
Basic |
MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE
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 do what
you can do" |
|
The Left function returns the requested number of characters
from the left side of the string.
|
Me.Print Left(strExample, 10) |
|
Returns, "Please try"
The Right function returns the requested number of characters
from the right side of the string.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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"
|
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"
CHANGING THE CASE A STRING:
The case of a string can be changed using either UCase or
LCase
UPPERCASE:
The following example produces a MessageBox with, "PLEASE
TRY TO DO WHAT YOU CAN DO".
|
Dim strExample As String
strExample = "Please try to do what you can do"
MsgBox UCase(strExample), vbInformation, "Uppercase" |
|
LOWERCASE:
The following example produces a MessageBox with, "Please
try to do what you can do".
|
Dim strExample As String
strExample = "Please try to do what you can do"
MsgBox LCase(strExample), vbInformation, "Lowercase" |
|
CREATING ARRAYS FROM STRINGS:
Sometimes it's necessary to break up strings into smaller strings,
for example if you wanted to submit a sentence as a list
of words to a database query. The Split function allows you
to break a string into an array, based on a delimiter. The first
parameter to the Split function is the string, the second
parameter is the delimiter. The function returns an array of the
elements that have been split out of the string. Using a delimiter
of a space will break out the words. The following example writes
each word in a TextBox called txtSentence and writes the
words down the form.
|
Dim list As Variant
Dim strSentence As String
Dim counter As Integer
strSentence = txtSentence.Text
list = Split(strSentence, " ")
For counter = 0 To UBound(list)
Me.Print list(counter)
Next counter |
|
CONVERTING DATA TO STRINGS:
The Str function may be used to convert a non-String data
type to a String data type.
|
Dim strNumWord As String
Const num As Integer = 64
strNumWord = Str(num) |
|
SELECTING TEXT IN A TEXTBOX:
The text in a TextBox can be highlighted using the Visual
Basic functions SelStart and SelLength functions.
SelStart specifies the start of the selection, and SelLength specifies
the number of characters to be selected.
The following example highlights whatever is in the TextBox
when it gains focus.
|
Private Sub txtSurname_GotFocus()
txtSurname.SelStart = 0
txtSurname.SelLength = Len(txtSurname.Text)
End Sub |
|
The next example, sets the text to "Lemon" and highlights
the portion "emo".
|
Private Sub txtSurname_GotFocus()
txtSurname.Text = "Lemon"
txtSurname.SelStart = 1
txtSurname.SelLength = 3
End Sub |
|
The SelText function returns the selected text.
|
MsgBox txtSurname.SelText, vbInformation,
"Selected Text" |
|
Use in the above function, the MsgBox would print "emo".
The following example uses the Selection functions to
create Copy, Cut and Paste menu items to copy, cut and paste from
whichever TextBoxes are used in the application.
MENU SOURCE CODE:
|
Option Explicit
Private Sub mnuFCopy_Click()
' Copy the selected text to the
Clipboard Object
Clipboard.Clear
Clipboard.SetText Screen.ActiveControl.SelText
End Sub
Private Sub mnuFCut_Click()
' Copy the selected text to the
Clipboard Object and remove
' the selected text
Clipboard.Clear
Clipboard.SetText Screen.ActiveControl.SelText
Screen.ActiveControl.SelText =
""
End Sub
Private Sub mnuFExit_Click()
Unload Me
End Sub
Private Sub mnuFPaste_Click()
' Paste the contents of the Clipboard
to the Selection
Screen.ActiveControl.SelText =
Clipboard.GetText
End Sub
Private Sub txtData_GotFocus(Index As Integer)
txtData(Index).SelStart = 0
txtData(Index).SelLength = Len(txtData(Index).Text)
End Sub |
|
|
If you don't find what you are looking for. Please click
here to submit your query, our experts will reply soon.
|