I adapted the following code from a contribution on the VB forum about
the mid-function:
'
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
'
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim str As String = "The quick brown fox jumps over the lazy dog."
Dim str2 As String = str
Dim result As String
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
'
result = leftString(str, 19)
Msgbox(result, "leftstring")
result = rightString(str, 4)
Msgbox(result, "rightstring")
result = Mid(str, 5, 15)
Msgbox(result, "Mid 1")
result = Mid(str, 1, 30)
Msgbox(result, "Mid 2")
'Mid$ in some old version
'E.G. Mid$(str,41,3)="cat" would replace dog with cat.
'The string.Replace is okay for a single instance of a word.>>
str = str.Replace("dog", "cat")
Msgbox(str, "Replace 1")
'It is NOT okay where two of the same word exist.>>
str = str.Replace("cat", "the")
'Now we have two of the word "the"
Msgbox(str, "Replace 2")
str = str.Replace("the", "very")
Msgbox(str, "Replace 3")
'Reset original string "str" to the original string.
str = str2
Msgbox(str,"str")
'Same as Mid$(str,5,5)="smart"
'The 2nd "5" is not needed as the FUNCTION uses the String.Length
str = Replace(str, 5, "smart")
Msgbox(str, "Replace 4")
End Sub
'
Sub leftString(aString, numOfChars)
aString = aString.Substring2(0, numOfChars)
Return aString
End Sub
'
Sub rightString(aString,numOfChars)
aString = aString.Substring(aString.Length - numOfChars)
aString = aString.Substring(aString.Length - numOfChars)
Return aString
End Sub
'
Sub Mid(aString, startPosition,numOfChars)
aString = aString.Substring2(startPosition - 1, numOfChars)
Return aString
End Sub
'
Sub Replace(aString, startPosition, newString)
Dim tempString As String = aString
aString = leftString(tempString, startPosition - 1)
aString = newString
aString = tempString.Substring(startPosition - 1 + newString.Length)
Return aString
End Sub
the mid-function:
'
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
'
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim str As String = "The quick brown fox jumps over the lazy dog."
Dim str2 As String = str
Dim result As String
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
'
result = leftString(str, 19)
Msgbox(result, "leftstring")
result = rightString(str, 4)
Msgbox(result, "rightstring")
result = Mid(str, 5, 15)
Msgbox(result, "Mid 1")
result = Mid(str, 1, 30)
Msgbox(result, "Mid 2")
'Mid$ in some old version
'E.G. Mid$(str,41,3)="cat" would replace dog with cat.
'The string.Replace is okay for a single instance of a word.>>
str = str.Replace("dog", "cat")
Msgbox(str, "Replace 1")
'It is NOT okay where two of the same word exist.>>
str = str.Replace("cat", "the")
'Now we have two of the word "the"
Msgbox(str, "Replace 2")
str = str.Replace("the", "very")
Msgbox(str, "Replace 3")
'Reset original string "str" to the original string.
str = str2
Msgbox(str,"str")
'Same as Mid$(str,5,5)="smart"
'The 2nd "5" is not needed as the FUNCTION uses the String.Length
str = Replace(str, 5, "smart")
Msgbox(str, "Replace 4")
End Sub
'
Sub leftString(aString, numOfChars)
aString = aString.Substring2(0, numOfChars)
Return aString
End Sub
'
Sub rightString(aString,numOfChars)
aString = aString.Substring(aString.Length - numOfChars)
aString = aString.Substring(aString.Length - numOfChars)
Return aString
End Sub
'
Sub Mid(aString, startPosition,numOfChars)
aString = aString.Substring2(startPosition - 1, numOfChars)
Return aString
End Sub
'
Sub Replace(aString, startPosition, newString)
Dim tempString As String = aString
aString = leftString(tempString, startPosition - 1)
aString = newString
aString = tempString.Substring(startPosition - 1 + newString.Length)
Return aString
End Sub