Professional Documents
Culture Documents
PROCESSING
AND
EXPLORING TEXT FILES
COMPARING STRINGS
Examples:
Comparison Results
"ABC" Like "A?C" True
"A3C" Like "A#C" True
"ABC" Like "A??C" False
"A1z3C" Like "A*C" True
"1B3" Like "1[A-Z]3" True
"123" Like "1[!A-Z]3" True
COMPARING STRINGS
Examples:
Comparison Results
"F" Like "FFF" False
"F" Like "f" False for Option Compare Binary
"F" Like "f" True for Option Compare Text
"aM5b" Like
True
"a[L-P]#[!c-e]"
"BAT123khg" Like
True
"B?T*"
COMPARING STRINGS
Return Value:
0 if strA and strB is in the same row in a sort order
1 if strA precedes strB in the sort order
-1 if strA comes after strB in the sort order
COMPARING STRINGS
SYNTAX:
strOut = Space(number of spaces)
SYNTAX:
lngCharCount = Len(strIn)
1. InStr Function
Can determine whether one string contains
another, and it can start looking at a specified
location in the string.
2. InstrRev Function
it searches strings similar to InStr function, except
that it searches from right to left. It is especially
used for finding file names on a path name.
THE STRING MODULE
SYNTAX: lngLocation = InStr([start], _
string1, string2 [,compare])
lngLocation = InstrRev(string1, _
string2 [,start] [,compare])
INSTR FUNCTION
Code Result
InStr(1,strSent,”is”) 3
InStr(4,strSent,”is”) 6
InStr(1,strSent,”IS”,0) 0
InStr(1,strSent,”IS”) 0
InStr(strSent,”IS”,1) 3
THE STRING MODULE
INSTRREV FUNCTION
Code Result
InstrRev(strPath,”\”) 27
InstrRev(strPath,”\”,26) 18
InstrRev(strSent,”\”,2) 0
THE STRING MODULE
Code Result
Microsoft.VisualBasic.Left(strProgName, 3) Vis
Microsoft.VisualBasic.Right(strProgName, 2) ic
Microsoft.VisualBasic.Left(strProgName, 6) Visual
Microsoft.VisualBasic.Right (strProgName, 5) Basic
Microsoft.VisualBasic.Left(strProgName, 8) Visual B
THE STRING MODULE
MID AS A “FUNCTION”
Syntax
Mid(string,start [,length])
MID AS A “FUNCTION”
Assume that the strProgName = “Visual Basic”
Code Result
Mid(strProgName, 2,1) i
Mid(strProgName, 4,2) ua
Mid(strProgName, 8) Basic
THE STRING MODULE
MID AS A “STATEMENT”
Syntax
MID AS A “STATEMENT”
Example :
This example uses the Mid statement to replace a
specified number of characters in a string variable with
characters from another string.
Dim TestString As String
' Initializes string.
TestString = "The dog jumps"
' Returns "The fox jumps".
Mid(TestString, 5, 3) = "fox"
' Returns "The cow jumps".
Mid(TestString, 5) = "cow"
' Returns "The cow jumpe".
Mid(TestString, 5) = "cow jumped over"
' Returns "The duc jumps".
Mid(TestString, 5, 3) = "duck"
THE STRING MODULE
SUBSTRING
Returns a fixed number of characters in a string
from a given starting point.
SPLIT
Splits a string into substrings based on a specified
separator and puts the substring in an array.
Example No.1
Dim AllText As String = "Visual Basic 2010"
Dim SplitText() As String
SplitText = AllText.Split(" ")
THE STRING CLASS
SPLIT
Example No.2
Dim AllText As String = _
"This is a sentence. This is a sentence too."
Dim SplitText() As String
SplitText = AllText.Split(".")
THE STRING CLASS
REPLACE
Replaces all instances of a substring in a string
with another string.
Example
Dim baseString = "Visual Basic *"
Dim newString As String = baseString.Replace("*", "2010")
Dim newString2 As String = _
newString.Replace("Basic", "Studio")
THE STRING CLASS
REPLACE
Dim base = txtBase.Text
Dim displayText As String = "The * equivalent of 10 is ?"
Dim baseText As String = ""
If base = 2 Then
baseText = "binary"
ElseIf base = 8 Then
baseText = "octal"
Else
baseText = "Base " & base.ToString
End If
STRING.REPLACE(OLDVALUE,NEWVALUE)
oldValue - The string to be replaced.
newValue - The string to replace ALL occurrences of
oldValue.
Return Value - A string that is equivalent to the current
string except that all instances of oldValue are replaced
with newValue. If oldValue is not found in the current
instance, the method returns the current instance
unchanged. If newValue is null, all occurrences of
oldValue are removed. Because this method returns the
modified string, you can chain together successive calls to
the Replace method to perform multiple replacements on
the original string.
Example
Dim s As String = "aaa"
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
' The final string: 'ddd‘
THE STRING CLASS
INDEXOF
Finds the index starting point of one string within a
larger string.
Example
Dim baseString = "Visual Basic"
Dim i1 As Byte = baseString.IndexOf("i")
Dim i2 As Byte = baseString.IndexOf("i",2)
THE STRING CLASS
STRING.INDEXOF(VALUE)
Return Value - The zero-based index position of
value if that string is found, or -1 if it is not. If value
is String.Empty, the return value is 0.
Example
Dim str As String = "animal"
Dim index As Integer = str.IndexOf("n")
'return value is 1
THE STRING CLASS
REMOVE
Removes characters from the middle of a
string.
Example
Dim RawStr, CleanStr As String
RawStr = "Hello333 there"
CleanStr = RawStr.Remove(5, 3)
STRING.REMOVE(STARTINDEX)
Returns a new string in which all the characters in the current
instance, beginning at a specified position and continuing
through the last position, have been deleted.
startIndex - The zero-based position to begin deleting
characters.
Return Value - A new string that is equivalent to this string
except for the removed characters.
Example
Dim s As String = "abc---def“
Console.WriteLine("Index: 012345678") 'Index: 012345678
Console.WriteLine("1) {0}", s) '1) abc---def
Console.WriteLine("2) {0}", s.Remove(3)) '2) abc
Console.WriteLine("3) {0}", s.Remove(3, 3)) '3) abcdef
THE STRING CLASS
INSERT
Adds characters to the middle of a string.
Dim base = 2
Dim displayText As String = "The equivalent of 10 is 1010"
Dim baseText As String = ""
STRING.INSERT(STARTINDEX,VALUE)
startIndex - The zero-based index position of the
insertion.
Value - The string to insert.
Return Value - A new string that is equivalent to this
instance, but with value inserted at position startIndex.
If startIndex is equal to the length of this instance,
value is appended to the end of this instance.
Example
Dim original As String = "aaabbb"
Dim modified As Integer = original.Insert(3, " ")
STRING.INSERT(STARTINDEX,VALUE)
startIndex - The zero-based index position of the
insertion.
Value - The string to insert.
Return Value - A new string that is equivalent to this
instance, but with value inserted at position startIndex.
If startIndex is equal to the length of this instance,
value is appended to the end of this instance.
Example
"abc".Insert(2, "XYZ") becomes "abXYZc"
"abc".Insert(2, "XYZ") becomes "abXYZc"