Difference between revisions of "Documentation/BASIC Guide/Strings (Runtime Library)"

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 11: Line 11:
 
When administering strings, {{OOo}} Basic uses the set of Unicode characters. The <tt>Asc</tt> and <tt>Chr</tt> functions allow the Unicode value belonging to a character to be established and/or the corresponding character to be found for a Unicode value. The following expressions assign the various Unicode values to the code variable:
 
When administering strings, {{OOo}} Basic uses the set of Unicode characters. The <tt>Asc</tt> and <tt>Chr</tt> functions allow the Unicode value belonging to a character to be established and/or the corresponding character to be found for a Unicode value. The following expressions assign the various Unicode values to the code variable:
  
Code = Asc("A")        ' Latin letter A (Unicode-value 65)
+
<source lang="oobas">
Code = Asc("€")        ' Euro character (Unicode-value 8364)
+
Code = Asc("A")        ' Latin letter A (Unicode-value 65)
Code = Asc("&#1083;")        ' Cyrillic letter &#1083; (Unicode-value 1083)
+
Code = Asc("€")        ' Euro character (Unicode-value 8364)
 +
Code = Asc("&#1083;")        ' Cyrillic letter &#1083; (Unicode-value 1083)
 +
</source>
  
 
Conversely, the expression  
 
Conversely, the expression  
  
MyString = Chr(13)
+
<source lang="oobas">
 +
MyString = Chr(13)
 +
</source>
  
 
ensures that the <tt>MyString</tt> string is initialized with the value of the number <tt>13</tt>, which stands for a hard line break.
 
ensures that the <tt>MyString</tt> string is initialized with the value of the number <tt>13</tt>, which stands for a hard line break.
Line 23: Line 27:
 
The <tt>Chr</tt> command is often used in Basic languages to insert control characters in a string. The assignment
 
The <tt>Chr</tt> command is often used in Basic languages to insert control characters in a string. The assignment
  
MyString = Chr(9) + "This is a test" + Chr(13)
+
<source lang="oobas">
 +
MyString = Chr(9) + "This is a test" + Chr(13)
 +
</source>
  
 
therefore ensures that the text is preceded by a tab character (Unicode-value 9) and that a hard line break (Unicode-value 13) is added after the text.  
 
therefore ensures that the text is preceded by a tab character (Unicode-value 9) and that a hard line break (Unicode-value 13) is added after the text.  
Line 38: Line 44:
 
Here are a few example calls for the named functions:  
 
Here are a few example calls for the named functions:  
  
Dim MyString As String
+
<source lang="oobas">
Dim MyResult As String
+
Dim MyString As String
Dim MyLen As Integer
+
Dim MyResult As String
+
Dim MyLen As Integer
MyString = "This is a small test"
+
 
MyResult = Left(MyString,5)      ' Provides the string "This "
+
MyString = "This is a small test"
MyResult = Right(MyString, 5)    ' Provides the string " test"
+
MyResult = Left(MyString,5)      ' Provides the string "This "
MyResult = Mid(MyString, 8, 5)  ' Provides the string " a sm"
+
MyResult = Right(MyString, 5)    ' Provides the string " test"
MyLen = Len(MyString)            ' Provides the value 21
+
MyResult = Mid(MyString, 8, 5)  ' Provides the string " a sm"
 +
MyLen = Len(MyString)            ' Provides the value 21
 +
</source>
  
 
== Search and Replace ==
 
== Search and Replace ==
Line 52: Line 60:
 
{{OOo}} Basic provides the <tt>InStr</tt> function for searching for a partial string within another string:
 
{{OOo}} Basic provides the <tt>InStr</tt> function for searching for a partial string within another string:
  
ResultString = InStr (MyString, SearchString)
+
<source lang="oobas">
 +
ResultString = InStr (MyString, SearchString)
 +
</source>
  
 
The <tt>SearchString</tt> parameter specifies the string to be searched for within <tt>MyString</tt>. The function returns a number that contains the position at which the <tt>SearchString</tt> first appears within <tt>MyString</tt>. If you want to find other matches for the string, the function also provides the opportunity to specify an optional start position from which {{OOo}} Basic begins the search. In this case, the syntax of the function is:
 
The <tt>SearchString</tt> parameter specifies the string to be searched for within <tt>MyString</tt>. The function returns a number that contains the position at which the <tt>SearchString</tt> first appears within <tt>MyString</tt>. If you want to find other matches for the string, the function also provides the opportunity to specify an optional start position from which {{OOo}} Basic begins the search. In this case, the syntax of the function is:
  
ResultString = InStr(StartPosition, MyString, SearchString)
+
<source lang="oobas">
 +
ResultString = InStr(StartPosition, MyString, SearchString)
 +
</source>
  
 
In the previous examples, <tt>InStr</tt> ignores uppercase and lowercase characters. To change the search so that <tt>InStr</tt> is case sensitive, add the parameter <tt>0</tt>, as shown in the following example:
 
In the previous examples, <tt>InStr</tt> ignores uppercase and lowercase characters. To change the search so that <tt>InStr</tt> is case sensitive, add the parameter <tt>0</tt>, as shown in the following example:
  
ResultString = InStr(MyString, SearchString, 0)
+
<source lang="oobas">
 +
ResultString = InStr(MyString, SearchString, 0)
 +
</source>
  
 
Using the previous functions for editing strings, programmers can search for and replace one string in another string:  
 
Using the previous functions for editing strings, programmers can search for and replace one string in another string:  
  
Function Replace(Source As String, Search As String, NewPart As String)
+
<source lang="oobas">
  Dim Result As String
+
Function Replace(Source As String, Search As String, NewPart As String)
  Dim StartPos As Long
+
  Dim Result As String
  Dim CurrentPos As Long
+
  Dim StartPos As Long
+
  Dim CurrentPos As Long
  Result = ""
+
 
  StartPos = 1
+
  Result = ""
  CurrentPos = 1
+
  StartPos = 1
+
  CurrentPos = 1
  If Search = "" Then
+
 
    Result = Source
+
  If Search = "" Then
  Else  
+
    Result = Source
    Do While CurrentPos <> 0
+
  Else  
      CurrentPos = InStr(StartPos, Source, Search)
+
    Do While CurrentPos <> 0
      If CurrentPos <> 0 Then
+
      CurrentPos = InStr(StartPos, Source, Search)
        Result = Result + Mid(Source, StartPos, _
+
      If CurrentPos <> 0 Then
        CurrentPos - StartPos)
+
        Result = Result + Mid(Source, StartPos, _
        Result = Result + NewPart
+
        CurrentPos - StartPos)
        StartPos = CurrentPos + Len(Search)
+
        Result = Result + NewPart
      Else
+
        StartPos = CurrentPos + Len(Search)
        Result = Result + Mid(Source, StartPos, Len(Source))
+
      Else
      End If                ' Position <> 0
+
        Result = Result + Mid(Source, StartPos, Len(Source))
    Loop  
+
      End If                ' Position <> 0
  End If  
+
    Loop  
+
  End If  
  Replace = Result
+
 
End Function
+
  Replace = Result
 +
End Function
 +
</source>
  
 
The function searches through the transferred <tt>Search</tt> string in a loop by means of <tt>InStr</tt> in the original term <tt>Source</tt>. If it finds the search term, it takes the part before the expression and writes it to the <tt>Result</tt> return buffer. It adds the new <tt>Part</tt> section at the point of the search term <tt>Search</tt>. If no more matches are found for the search term, the function establishes the part of the string still remaining and adds this to the return buffer. It returns the string produced in this way as the result of the replacement process.
 
The function searches through the transferred <tt>Search</tt> string in a loop by means of <tt>InStr</tt> in the original term <tt>Source</tt>. If it finds the search term, it takes the part before the expression and writes it to the <tt>Result</tt> return buffer. It adds the new <tt>Part</tt> section at the point of the search term <tt>Search</tt>. If no more matches are found for the search term, the function establishes the part of the string still remaining and adds this to the return buffer. It returns the string produced in this way as the result of the replacement process.
Line 96: Line 112:
 
Since replacing parts of character sequences is one of the most frequently used functions, the <tt>Mid</tt> function in {{OOo}} Basic has been extended so that this task is performed automatically. The following example replaces three characters with the string <tt>is</tt> from the sixth position of the <tt>MyString</tt> string.
 
Since replacing parts of character sequences is one of the most frequently used functions, the <tt>Mid</tt> function in {{OOo}} Basic has been extended so that this task is performed automatically. The following example replaces three characters with the string <tt>is</tt> from the sixth position of the <tt>MyString</tt> string.
  
Dim MyString As String
+
<source lang="oobas">
 +
Dim MyString As String
 
   
 
   
MyString <nowiki>= "This was my text"</nowiki>
+
MyString = "This was my text"
Mid(MyString, 6, 3, "is")
+
Mid(MyString, 6, 3, "is")
 +
</source>
  
 
== Formatting Strings ==
 
== Formatting Strings ==
Line 111: Line 129:
 
The example below shows how the <tt>0</tt> and <tt>.</tt> characters can define the digits after the decimal point in an expression:
 
The example below shows how the <tt>0</tt> and <tt>.</tt> characters can define the digits after the decimal point in an expression:
  
MyFormat = "0.00"
+
<source lang="oobas">
MyString = Format(-1579.8, MyFormat)    ' Provides "-1579,80"
+
MyFormat = "0.00"
MyString = Format(1579.8, MyFormat)      ' Provides "1579,80"
+
MyString = Format(-1579.8, MyFormat)    ' Provides "-1579,80"
MyString = Format(0.4, MyFormat)        ' Provides "0,40"
+
MyString = Format(1579.8, MyFormat)      ' Provides "1579,80"
MyString = Format(0.434, MyFormat)      ' Provides "0,43"
+
MyString = Format(0.4, MyFormat)        ' Provides "0,40"
 +
MyString = Format(0.434, MyFormat)      ' Provides "0,43"
 +
</source>
  
 
In the same way, zeros can be added in front of a number to achieve the desired length:
 
In the same way, zeros can be added in front of a number to achieve the desired length:
  
MyFormat = "0000.00"
+
<source lang="oobas">
MyString = Format(-1579.8, MyFormat)    ' Provides "-1579,80"
+
MyFormat = "0000.00"
MyString = Format(1579.8, MyFormat)      ' Provides "1579,80"
+
MyString = Format(-1579.8, MyFormat)    ' Provides "-1579,80"
MyString = Format(0.4, MyFormat)        ' Provides "0000,40"
+
MyString = Format(1579.8, MyFormat)      ' Provides "1579,80"
MyString = Format(0.434, MyFormat)      ' Provides "0000,43"
+
MyString = Format(0.4, MyFormat)        ' Provides "0000,40"
 +
MyString = Format(0.434, MyFormat)      ' Provides "0000,43"
 +
</source>
  
 
A <tt>,</tt> represents the character that the operating system uses for a thousands separator, and the <tt>#</tt> stands for a digit or place that is only displayed if it is required by the input string.
 
A <tt>,</tt> represents the character that the operating system uses for a thousands separator, and the <tt>#</tt> stands for a digit or place that is only displayed if it is required by the input string.
  
MyFormat = "#,##0.00"
+
<source lang="oobas">
MyString = Format(-1579.8, MyFormat)  ' Provides "-1.579,80"
+
MyFormat = "#,##0.00"
MyString = Format(1579.8, MyFormat)      ' Provides "1.579,80"
+
MyString = Format(-1579.8, MyFormat)  ' Provides "-1.579,80"
MyString = Format(0.4, MyFormat)      ' Provides "0,40"
+
MyString = Format(1579.8, MyFormat)      ' Provides "1.579,80"
MyString = Format(0.434, MyFormat)      ' Provides "0,43"
+
MyString = Format(0.4, MyFormat)      ' Provides "0,40"
 +
MyString = Format(0.434, MyFormat)      ' Provides "0,43"
 +
</source>
  
 
In place of the <tt>$</tt> place holder, the <tt>Format</tt> function displays the relevant currency symbol defined by the system:
 
In place of the <tt>$</tt> place holder, the <tt>Format</tt> function displays the relevant currency symbol defined by the system:
  
MyFormat = "#,##0.00 $"   
+
<source lang="oobas">
MyString = Format(-1579.8, MyFormat)  ' Provides "-1.579,80 —"  
+
MyFormat = "#,##0.00 $"   
MyString = Format(1579.8, MyFormat)      ' Provides "1.579,80 —"  
+
MyString = Format(-1579.8, MyFormat)  ' Provides "-1.579,80 —"  
MyString = Format(0.4, MyFormat)      ' Provides "0,40 —"  
+
MyString = Format(1579.8, MyFormat)      ' Provides "1.579,80 —"  
MyString = Format(0.434, MyFormat)      ' Provides "0,43 —"  
+
MyString = Format(0.4, MyFormat)      ' Provides "0,40 —"  
 +
MyString = Format(0.434, MyFormat)      ' Provides "0,43 —"  
 +
</source>
  
 
{{Documentation/Note|The format instructions used in VBA for formatting date and time details are not supported in {{OOo}} Basic.}}
 
{{Documentation/Note|The format instructions used in VBA for formatting date and time details are not supported in {{OOo}} Basic.}}
  
 
{{PDL1}}
 
{{PDL1}}

Revision as of 13:25, 2 April 2008

Working with Sets of Characters

When administering strings, Apache OpenOffice Basic uses the set of Unicode characters. The Asc and Chr functions allow the Unicode value belonging to a character to be established and/or the corresponding character to be found for a Unicode value. The following expressions assign the various Unicode values to the code variable:

Code = Asc("A")         ' Latin letter A (Unicode-value 65)
Code = Asc("€")         ' Euro character (Unicode-value 8364)
Code = Asc("&#1083;")         ' Cyrillic letter &#1083; (Unicode-value 1083)

Conversely, the expression

MyString = Chr(13)

ensures that the MyString string is initialized with the value of the number 13, which stands for a hard line break.

The Chr command is often used in Basic languages to insert control characters in a string. The assignment

MyString = Chr(9) + "This is a test" + Chr(13)

therefore ensures that the text is preceded by a tab character (Unicode-value 9) and that a hard line break (Unicode-value 13) is added after the text.

Accessing Parts of a String

Apache OpenOffice Basic provides four functions that return partial strings:

Left(MyString, Length)
returns the first Length characters of MyString.
Right(MyString, Length)
returns the last Length characters of MyString.
Mid(MyString, Start, Length)
returns first Length characters of MyString as of the Start position.
Len(MyString)
returns the number of characters in MyString.

Here are a few example calls for the named functions:

Dim MyString As String
Dim MyResult As String
Dim MyLen As Integer
 
MyString = "This is a small test"
MyResult = Left(MyString,5)      ' Provides the string "This "
MyResult = Right(MyString, 5)    ' Provides the string " test"
MyResult = Mid(MyString, 8, 5)   ' Provides the string " a sm"
MyLen = Len(MyString)            ' Provides the value 21

Search and Replace

Apache OpenOffice Basic provides the InStr function for searching for a partial string within another string:

ResultString = InStr (MyString, SearchString)

The SearchString parameter specifies the string to be searched for within MyString. The function returns a number that contains the position at which the SearchString first appears within MyString. If you want to find other matches for the string, the function also provides the opportunity to specify an optional start position from which Apache OpenOffice Basic begins the search. In this case, the syntax of the function is:

ResultString = InStr(StartPosition, MyString, SearchString)

In the previous examples, InStr ignores uppercase and lowercase characters. To change the search so that InStr is case sensitive, add the parameter 0, as shown in the following example:

ResultString = InStr(MyString, SearchString, 0)

Using the previous functions for editing strings, programmers can search for and replace one string in another string:

Function Replace(Source As String, Search As String, NewPart As String)
  Dim Result As String
  Dim StartPos As Long
  Dim CurrentPos As Long
 
  Result = ""
  StartPos = 1
  CurrentPos = 1
 
  If Search = "" Then
    Result = Source
  Else 
    Do While CurrentPos <> 0
      CurrentPos = InStr(StartPos, Source, Search)
      If CurrentPos <> 0 Then
        Result = Result + Mid(Source, StartPos, _
        CurrentPos - StartPos)
        Result = Result + NewPart
        StartPos = CurrentPos + Len(Search)
      Else
        Result = Result + Mid(Source, StartPos, Len(Source))
      End If                ' Position <> 0
    Loop 
  End If 
 
  Replace = Result
End Function

The function searches through the transferred Search string in a loop by means of InStr in the original term Source. If it finds the search term, it takes the part before the expression and writes it to the Result return buffer. It adds the new Part section at the point of the search term Search. If no more matches are found for the search term, the function establishes the part of the string still remaining and adds this to the return buffer. It returns the string produced in this way as the result of the replacement process.

Since replacing parts of character sequences is one of the most frequently used functions, the Mid function in Apache OpenOffice Basic has been extended so that this task is performed automatically. The following example replaces three characters with the string is from the sixth position of the MyString string.

Dim MyString As String
 
MyString = "This was my text"
Mid(MyString, 6, 3, "is")

Formatting Strings

The Format function formats numbers as a string. To do this, the function expects a Format expression to be specified, which is then used as the template for formatting the numbers. Each place holder within the template ensures that this item is formatted correspondingly in the output value. The five most important place holders within a template are the zero (0), pound sign (#), period (.), comma (,) and dollar sign ($) characters.

The 0 character within the template ensures that a number is always placed at the corresponding point. If a number is not provided, 0 is displayed in its place.

A . stands for the decimal point symbol defined by the operating system in the country-specific settings.

The example below shows how the 0 and . characters can define the digits after the decimal point in an expression:

MyFormat = "0.00"
MyString = Format(-1579.8, MyFormat)     ' Provides "-1579,80"
MyString = Format(1579.8, MyFormat)      ' Provides "1579,80"
MyString = Format(0.4, MyFormat)         ' Provides "0,40"
MyString = Format(0.434, MyFormat)       ' Provides "0,43"

In the same way, zeros can be added in front of a number to achieve the desired length:

MyFormat = "0000.00"
MyString = Format(-1579.8, MyFormat)     ' Provides "-1579,80"
MyString = Format(1579.8, MyFormat)      ' Provides "1579,80"
MyString = Format(0.4, MyFormat)         ' Provides "0000,40"
MyString = Format(0.434, MyFormat)       ' Provides "0000,43"

A , represents the character that the operating system uses for a thousands separator, and the # stands for a digit or place that is only displayed if it is required by the input string.

MyFormat = "#,##0.00"
MyString = Format(-1579.8, MyFormat)   ' Provides "-1.579,80"
MyString = Format(1579.8, MyFormat)      ' Provides "1.579,80"
MyString = Format(0.4, MyFormat)      ' Provides "0,40"
MyString = Format(0.434, MyFormat)      ' Provides "0,43"

In place of the $ place holder, the Format function displays the relevant currency symbol defined by the system:

MyFormat = "#,##0.00 $"   
MyString = Format(-1579.8, MyFormat)   ' Provides "-1.579,80 —" 
MyString = Format(1579.8, MyFormat)      ' Provides "1.579,80 —" 
MyString = Format(0.4, MyFormat)      ' Provides "0,40 —" 
MyString = Format(0.434, MyFormat)      ' Provides "0,43 —"

Template:Documentation/Note

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools