21.08.2013 Views

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Strings 49<br />

The SearchString parameter specifies the string to be searched for within MyString. The<br />

function returns a number that contains the position at which the SearchString first<br />

appears within MyString. If you want to find other matches for the string, the function also<br />

provides the opportunity to specify an optional start position from which <strong>OpenOffice</strong>.<strong>org</strong><br />

Basic begins the search. In this case, 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<br />

search so that InStr is case sensitive, add the parameter 0, as shown in the following<br />

example:<br />

ResultString = InStr(MyString, SearchString, 0)<br />

Using the previous functions for editing strings, programmers can search for and replace<br />

one string in another 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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!