Substrings


Use the Left function to retrieve a portion of the string from the start. The first argument is the string from which to extract the characters, and the second argument indicates how many characters to return. Analogously, the Right function returns strings from the end of a string. If the requested length is zero, an empty string is returned. If the requested length is too large, the entire string is returned.

 Print  Left("12345", 2)  '12 Print  Left("12345", 8)  '12345 Print Right("12345", 2)  '45 

The length argument for the Left and Right functions is an integer. A string can contain up to 65,535 characters (see Listing 3 ). An integer variable cannot be larger than 32,767. The Right function correctly handles strings up to 65,535 characters, but the Left function generates a run-time error for string-length arguments larger than 32,767.

Listing 3: Strings can contain up to 65,535 characters; this is too large for an integer argument.
start example
 Dim s1 As String s1 = String(44002, "*")     'This string has 44002 characters Print Len(s1)               '44002 Print Len(Left(s1, 44000))  'Run-time error: Invalid procedure call! Print Len(Right(s1, 44000)) '44000 
end example
 
Bug  

As of OOo version 1.1.1, the Left function cannot return a string longer than 32,767 characters. Use the Mid function for large strings instead.

Use the Mid function to extract arbitrary substrings and to replace substrings in an existing string. In general, the string functions return a new string rather than modifying the existing string. For example, the Trim function returns a new string with leading and trailing spaces removed, rather than removing leading and trailing spaces from the existing string. The Mid function, however, can be used to modify the string rather than simply returning a new one. In its simplest form, the Mid function has the same functionality as the Right function. The first argument is a string, and the second argument is a starting position. The optional third argument indicates the length of the string to return.

 Print Mid("123456", 3)        '3456 Print Mid("123456", 3, 2)     '34 s1 = String(44000, "*")&"XX" Print Mid(s1, 44000)          '*XX    No problem with large arguments Print Len(Mid(s1, 2, 40000))  '40000  No problem with large arguments 

The Mid function has no problems with strings larger than 32,767, and it can provide the same functionality as the Left function.

 Left(s, n) = Mid(s, 1, n) 

The Mid function takes an optional fourth argument, a string, that replaces the specified substring in the first argument. In other words, if four arguments are present, the first three arguments specify a substring in the string and the fourth argument replaces the substring. This may shorten the length of a string, but in OOo Basic this will never cause the string to become longer. If the final argument is longer than the specified substring, only the first characters are used from the final argument to replace the substring.

 s = "123456789" Mid(s, 3, 5, "")        'Replace five characters with nothing Print s                 '1289 s = "123456789" Mid(s, 3, 5, "XX")      'Replace five characters with two Print s                 '12XX89 s = "123456789" Mid(s, 3, 5, "ABCDEFG") 'Cannot add more than you replace from the middle Print s                 '12ABCDE89 s = "123456789" Mid(s, 7, 12, "ABCDEFG")'You can add more than you remove from the end Print s                 '123456ABCDEFG 

The ReplacelnString function (see Listing 4 ) emulates the Mid function, with two exceptions: It puts the entire new string in place, even if it's longer than the substring that it replaces, and it doesn't modify the original string.

Listing 4: ReplacelnString is found in the String module in this chapter's source code files as SC06.sxw.
start example
 REM This function is similar to Mid with four arguments. REM This function does not modify the original string. REM This function handles replacment text larger than n. REM Left is not used so this handles large values of i and n. Function ReplaceInString(s$, i&, n&, sNew$) As String   If i <= 1 Then     'Place the string in front.     'The only question is how much string must be removed.     If n < 1 Then                      'Remove nothing       ReplacelnString = sNew & s     ElseIf n >= Len(s) Then            'Remove everything       ReplacelnString = sNew     Else                               'Remove a portion from the left       ReplaceInString = sNew & Right(s, Len(s) - n)     End If     ElseIf i + n > Len(s) Then       'Replacing past the end, then extract the leftmost parts       'Mid works to find if the length argument is larger than the       'string, so this is fine! Append the new text to the end.       ReplaceInString = Mid(s, 1, i - 1) & sNew     Else       'Replace somewhere in the middle of the string.       'First, obtain the leftmost text.       'Second, insert the new text, if any.       'Finally, obtain the rightmost text.       ReplaceInString = Mid(s, 1, i - 1) & sNew & Right(s, Len(s) - i - n + 1)      End If End Function 
end example
 



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