Standard String Functions


The basic comparison operators (<, <=, >, >=, and <>) work with strings as well as numbers . They perform case-sensitive comparisons. This means that the strings "a" and "A" are not considered to be the same. You can also use the StrComp function to compare strings; it defaults to performing case-sensitive comparisons. The StrComp function returns -1, 0, or 1 if the first string argument is less than, equal to, or greater than the second string argument. Set the optional third argument to zero for a case-insensitive comparison.

My wife really likes to file. I also like to file, but my wife is the real filer in the family. She likes her filing cabinets and she keeps the files in alphabetical order-well, she usually does. Besides my wife's files, the telephone book is another place to look for an alphabetized list. The name "AA" comes before "AAA", because although the letters are the same, "AA" is shorter than "AAA". The StrComp function uses a similar method to compare strings. The following code gives an idea of how StrComp works for a case-sensitive comparison. A case-insensitive comparison would simply convert both strings to all uppercase before performing the comparison.

 Let s1 = string1 Let s2 = string2 Let min_len = minimum(Len(s1), Len(s2)) For i = 1 To min_len   If ASC(Mid(sl, i, 1)) < ASC(Mid(s2, i, 1)) Then     set return value to -1     Exit Function   End If   If ASC(Mid(sl, i, 1)) > ASC(Mid(s2, i, 1)) Then     set return value to -1     Exit Function   End If Next If Len(sl) < Len(s2) Then   set return value to -1   Exit Function End If If Len(sl) > Len(s2) Then   set return value to 1   Exit Function End If set return value to 0 Exit Function 

The numerical Unicode value of the first character in the first string is compared to the numerical Unicode value of the first character in the second string. If the first character is numerically less than the second character, -1 is returned. If the first character is numerically greater than the second character, 1 is returned. If the first character in each string is the same, the second character in each string is compared. If the corresponding numerical Unicode value of each character is the same and the strings are the same length, 0 is returned. If corresponding characters all match, but the strings are of different lengths, the shorter string is considered less than the longer string.

 Print StrComp( "A", "AA")  '-1 because  "A" < "AA" Print StrComp("AA", "AA")  ' 0 because "AA" = "AA" Print StrComp("AA", "A")   ' 1 because "AA" >  "A" Print StrComp( "a", "A")   ' 1 because  "a" > "A" Print StrComp( "a", "A", 1)' 1 because  "a" > "A" Print StrComp( "a", "A", 0)' 0 because  "a" = "A" if case is ignored 

Use the UCase and LCase functions to return a copy of the string with all characters in uppercase or lowercase.

 S$ = "Las Vegas" Print LCase(s) REM Returns "las vegas" Print UCase(s) REM Returns "LAS VEGAS" 

If numerous comparisons will be made, using LCase or UCase is sometimes faster than performing a case-insensitive comparison each time. And sometimes, it is simply easier to use.

 If LCase(Right(sFileName, 3)) = "sxw" Then 

Use the LTrim, RTrim, and Trim functions to return copies of a string with leading, trailing, or both leading and trailing spaces removed. I usually do this with data retrieved from files and databases, and directly from users. The original string is unchanged, as are all internal spaces. Some trim routines in other programming languages trim all sorts of invisible characters, such as carriage returns, new-line characters, and tabs. In OOo Basic, only the space character with an ASCII value of 32 is trimmed .

 s = " hello world " Print "(" & LTrim(s) & ")" '(hello  world   ) Print "(" & RTrim(s) & ")" '(  hello  world) Print "(" & Trim(s) & ")"  '(hello  world) 

Use the Len function to return the number of characters in the string. If the argument is not a string, it is converted to a string first. It's probably safer to use CStr to convert nonstring arguments to strings, rather than to rely on the automatic behavior. For example, types such as Byte will not convert automatically as expected. The value held in the Byte data type is treated as an ASCII value and converted to a single character. The CStr function avoids this problem.

 Print Len("")    '0 Print Len("1")   '1 Print Len("123") '3 Print Len(12)    '2 the number is converted to a string 

To create a string with a single character repeated multiple times, use the String function. The first argument is an integer indicating how many times the character is repeated. Zero is a valid value for the first argument, returning an empty string. The second argument is the character to repeat. Just like the ASC function, the String function uses the first character from the string and ignores the rest. If the second argument is a number, it's treated as an ASCII value and the character is created from the number.

 Print String(2, 65)    'AA 65 is ASCII for A Print String(2, "AB")  'AA Only the first character is used Print ASC(String(2))   '0  Bug: Created string with two ASCII 0 characters Print Len(Space(4))    '4  Four spaces 
Warning  

Do not use a negative first argument to the String function; OOo dutifully attempts to comply and never comes back.

Use the function InStr to find where (and if) one string is contained inside another. The InStr function can take four arguments. The first argument is an optional integer that indicates the first character to check. This defaults to 1, the first character, if it is not included. InStr then searches the second argument to see if it contains the third argument. The fourth optional argument determines if the comparison is case sensitive (0) or case insensitive (1). The default search is case sensitive. You can't use the fourth argument unless you also use the first argument.

Warning  

The StrComp function uses a 0 to indicate a case-insensitive comparison and a 1-the default-to indicate a case-sensitive comparison. The InStr function, however, uses a 0 to indicate a case-sensitive comparison and a 1-the default-to indicate a case-insensitive comparison. The only similarity is that the value 1 is the default.

 Print InStr("CBAABC", "abc")       '4 default to case insensitive Print InStr(1, "CBAABC", "b")      '2 first argument is 1 by default Print InStr(2, "CBAABC", "b")      '2 start with second character Print InStr(3, "CBAABC", "b")      '5 start with third character Print InStr(1, "CBAABC", "b", 0)   '0 case-sensitive comparison Print InStr(1, "CBAABC", "b", 1)   '2 case-insensitive comparison Print InStr(1, "CBAABC", "B", 0)   '2 case-sensitive comparison 

The return value from InStr is an integer, which is limited to values from -32,768 through 32,767. A string can be up to 65,535 characters in length, which causes problems when InStr searches large strings.

 Dim s1 As String s1 = String(44000, "*") & "XX"  'This string has 44002 characters Print InStr(s1, "XX")           '-21535 rather than 44001 
Bug  

As of version 1.1.1, the InStr function returns an Integer rather than a Long, causing problems with large strings.




OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net