OpenOffice.org BASIC Guide - OpenOffice.org wiki
OpenOffice.org BASIC Guide - OpenOffice.org wiki
OpenOffice.org BASIC Guide - OpenOffice.org wiki
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Search and Replace<br />
<strong>OpenOffice</strong>.<strong>org</strong> Basic provides the InStr function for searching for a partial string within another string:<br />
ResultString = InStr (MyString, SearchString)<br />
Strings<br />
The SearchString parameter specifies the string to be searched for within MyString. The function returns a<br />
number that contains the position at which the SearchString first appears within MyString; a return value of<br />
zero indicates no match. If you want to find other matches for the string, the function also provides the<br />
opportunity to specify an optional start position from which <strong>OpenOffice</strong>.<strong>org</strong> Basic begins the search. In this case,<br />
the syntax of the function is:<br />
ResultString = InStr(StartPosition, MyString, SearchString)<br />
In the previous examples, InStr ignores uppercase and lowercase characters. To change the search so that InStr<br />
is case sensitive, add the parameter 0, as shown in the following example:<br />
ResultString = InStr(MyString, SearchString, 0)<br />
Using the previous functions for editing strings, programmers can search for and replace one string in another<br />
string:<br />
Function Replace(Source As String, Search As String, NewPart As String)<br />
Dim Result As String<br />
Dim StartPos As Long<br />
Dim CurrentPos As Long<br />
Result = ""<br />
StartPos = 1<br />
CurrentPos = 1<br />
If Search = "" Then<br />
Result = Source<br />
Else<br />
Do While CurrentPos 0<br />
CurrentPos = InStr(StartPos, Source, Search)<br />
If CurrentPos 0 Then<br />
Result = Result + Mid(Source, StartPos, _<br />
CurrentPos - StartPos)<br />
Result = Result + NewPart<br />
StartPos = CurrentPos + Len(Search)<br />
Else<br />
Result = Result + Mid(Source, StartPos, Len(Source))<br />
End If ' Position 0<br />
Loop<br />
End If<br />
Replace = Result<br />
End Function<br />
The function searches through the transferred Search string in a loop by means of InStr in the original term<br />
Source. If it finds the search term, it takes the part before the expression and writes it to the Result return<br />
buffer. It adds the NewPart section at the point of the search term Search. If no more matches are found for the<br />
search term, the function establishes the part of the string still remaining and adds this to the return buffer. It<br />
returns the string produced in this way as the result of the replacement process.<br />
Since replacing parts of character sequences is one of the most frequently used functions, the Mid function in<br />
<strong>OpenOffice</strong>.<strong>org</strong> Basic has been extended so that this task is performed automatically. The following example<br />
replaces three characters with the string is from the sixth position of the MyString string.<br />
Dim MyString As String<br />
MyString = "This was my text"<br />
Mid(MyString, 6, 3, "is")<br />
Warning – When it is used with 4 arguments, to replace a sub-string in a string, Mid is an instruction, not a<br />
function : it does not return any value !<br />
Chapter 3 · Runtime Library 39