このプログラムでは、LotusScript® 配列関数の ArrayAppend、ArrayGetIndex、ArrayReplace、FullTrim の機能と使用法を説明します。また、String 関数の StrLeft、StrRight、StrLeftBack、StrRightBack の機能と使用法も説明します。
重要なコードは ArrayExamples と AtComputeStrings という 2 つのルーチンにあります。残りの部分は宣言と初期化です。コードから生成される出力も以下にリストしてあります。
StringExample:
Option Public
Option Base 1
Dim arr1(8) As String
Dim arr2(8) As String
Dim arr3
Dim arr4(8) As Integer
Dim tarray1(10) As Integer
Dim tarray2(10) As Integer
Dim tarray3(10) As Integer
Dim i As Integer, x As Integer
Dim ans As String
Dim Indexresult
Dim localarray
Dim arresult
Sub Initialize
' arr1 will contain the following names
arr1(1) = "Daniel"
arr1(2) = "Nate"
arr1(3) = "Joshua"
arr1(4) = "Sam"
arr1(5) = "Benjamin"
arr1(6) = "Julie "
arr1(7) = "Lauren "
arr1(8) = "Scrubbles"
' arr2 will contain "Joe1", "Joe2", etc
' arr4 will contain integers, with all even
' entries being zero
For i = 1 To 8
arr2(i) = "Joe" & i
If (i Mod 2) = 0 Then
arr4(i) = 0
Else
arr4(i) = i
End If
Next
' Initialize the arrays
' tarray1 will contain (1,2,3,4,5,6,7,8,9,10)
x = 1
For i =1 To 20
If i =< 10 Then
tarray1(i) = i
End If
' tarray2 will contain (2,4,6,8,10,12,14,16,18,20)
If (i Mod 2) = 0 Then
tarray2(x) = i
x = x+1
End If
Next
' tarray3 will contain the following (8,6,4,2,25,0,0,0,0,0)
tarray3(1) = 8
tarray3(2) = 6
tarray3(3) = 4
tarray3(4) = 2
tarray3(5) = 25
' Run the examples
ArrayExamples
AtComputeStrings
End Sub
Sub Arrayexamples
' Arrayappend populates arr3 with all elements of arr1
' and all elements of arr2, arr3 lower bound is 1
' its upper bound is 16
Print "Arrayappend results:"
arr3 = Arrayappend (arr1, arr2)
Print " arr3 contains: ", arr3(1), arr3(2), "..." , _
arr3(15), arr3(16)
Print " Up/Low bounds for arr3: " & Lbound(arr3) & _
" & " & Ubound(arr3)
' Arraygetindex example
value = "Benjamin"
Indexresult = Arraygetindex(arr1,value)
Print "Arraygetindes results:"
Print " Arraygetindex(arr1,value) returns ";_
Indexresult
Indexresult = Arraygetindex(arr1,"Scrubbles")
Print " Arraygetindex(arr1,""Scrubbles"") returns "; Indexresult
' Fulltrim of an array
Print "Arraygetindex on fulltrimed array results:"
localarray = Fulltrim(arr4) 'localarray = [1, 3, 5, 7]
Indexresult = Arraygetindex(localarray, 3)
Print " Arraygetindex(localarray, 3) returns "; Indexresult
'Fulltrim of a string
Print "Fulltrim of string:"
qbf_spaces = " The quick brown fox" & _
" jumped over the lazy dog. "
Print " ", qbf_spaces
qbf_trimed = Fulltrim(qbf_spaces)
Print " ", qbf_trimed
' Arrayreplace example
Print "Arrayreplace results:"
'Expected answer is "1 8 3 6 5 4 7 2 9 25"
arresult = Arrayreplace( tarray1, tarray2, tarray3)
' Generate string that represent the contents of arresult
msg1 = ""
For i = 1 To 10
msg1 = msg1 & " " & arresult(i)
Next
Print " arresult = " & msg1
End Sub
Sub AtComputeStrings()
Dim s1 As String
Dim s2 As String
Dim v1 As Variant
s1 = "The quick brown FOX jumps over the lazy dog."
s2 = "he"
Print " "
Print "Results for Strleft, strright, strrightback," & _
" Strleftback"
' Search left to right
' Strleft: expect v1 = "T"
v1 = Strleft( s1, s2 )
Print " " + v1
' Strright:
' expect v1 = " quick brown FOX jumps over the lazy dog."
v1 = Strright( s1,s2 )
Print " " + v1
' Search right to left
' Strleftback:
' expect v1 = "The quick brown FOX jumps over t"
v1 = Strleftback( s1, s2 )
Print " " + v1
' Strrightback: expect v1 = " lazy dog."
v1 = Strrightback( s1, s2 )
Print " " + v1
' With some optionals.....
s1 = "The quick brown FOX jumps over the lazy dog."
s2 = "o"
' the letter o CHANGED S2, pattern searched for HERE.
' A case INsensitive search, it finds the second
' occurrence of 'o' and returns what is to the left of that.
' expect v1 = "The quick brown F "
v1 = Strleft( s1,s2, 5, 2 )
Print " " + v1
' A Case sensitive search, Finds the third occurrence of
' 'o' and returns what is to the RIGHT of that.
' expect v1 = "ver the lazy dog."
v1 = Strright( s1,s2, 0, 3 )
Print " " + v1
s2 = "O"
' A case sensitive search. Expect v1 = "The quick brown F"
v1 = Strleftback( s1,s2,0 )
Print " " + v1
' A case sensitive search, with a skip first occurrence,
' O in FOX is Skipped and no other occurrence exists,
' expect v1 = ""
v1 = Strleft( s1,s2,0,2)
Print " " + v1
End Sub
このプログラムの結果:
ArrayAppend の結果:
arr3 contains: Daniel, Nate, ... Joe7, Joe8
Up/Low bounds for arr3: 1 & 16
ArrayGetIndex の結果:
ArrayGetIndex(arr1,value) returns 5
Arraygetindex(arr1,"Scrubbles") returns 8
空白が削除された配列に対する ArrayGetIndex の結果:
Arraygetindex(localarray, 3) returns 2
文字列の FullTrim:
The quick brown fox jumped over the lazy dog.
The quick brown fox jumped over the lazy dog.
ArrayReplace の結果:
arresult = 1 8 3 6 5 4 7 2 9 25
Strleft、strright、strrightback、Strleftback の結果
T
quick brown FOX jumps over the lazy dog.
The quick brown FOX jumps over t
lazy dog.
The quick brown F
g.
The quick brown F