Difference between revisions of "ZH/Documentation/BASIC Guide/Strings (Runtime Library)"
(New page: {{DISPLAYTITLE:字符串({{OOo}} BASIC 运行时库)}} {{ZH/Documentation/BASICGuideTOC/v2 |ShowPrevNext=block |ShowPrevPage=block |PrevPage=Documentation/BASIC Guide/Conversion Functi...) |
|||
Line 3: | Line 3: | ||
|ShowPrevNext=block | |ShowPrevNext=block | ||
|ShowPrevPage=block | |ShowPrevPage=block | ||
− | |PrevPage=Documentation/BASIC Guide/Conversion Functions (Runtime Library) | + | |PrevPage=ZH/Documentation/BASIC Guide/Conversion Functions (Runtime Library) |
− | |NextPage=Documentation/BASIC Guide/Date and Time (Runtime Library) | + | |NextPage=ZH/Documentation/BASIC Guide/Date and Time (Runtime Library) |
|runtime=block | |runtime=block | ||
}} | }} | ||
Line 179: | Line 179: | ||
</source> | </source> | ||
{{PDL1}} | {{PDL1}} | ||
+ | {{InterWiki_Languages_BasicGuide|articletitle=Documentation/BASIC Guide/Strings (Runtime Library)}} | ||
{{InterWiki_Languages_BasicGuide|articletitle=Documentation/BASIC Guide/Strings (Runtime Library)}} | {{InterWiki_Languages_BasicGuide|articletitle=Documentation/BASIC Guide/Strings (Runtime Library)}} |
Revision as of 22:08, 12 December 2008
使用字符集
在管理字符串时,Apache OpenOffice Basic 使用 Unicode 字符集。Asc 和 Chr 函数用于确定属于字符的 Unicode 值和/或查找 Unicode 值对应的字符。以下表达式将不同 Unicode 值赋值给 Code 变量:
Code = Asc("A") ' Latin letter A (Unicode-value 65)
Code = Asc("€") ' Euro character (Unicode-value 8364)
Code = Asc("Л") ' Cyrillic letter Л (Unicode-value 1083)
相反,表达式
MyString = Chr(13)
确保使用数字值 13(表示硬换行符)初始化 MyString 字符串。
Basic 语言经常使用 Chr 命令在字符串中插入控制字符。因此,赋值语句
MyString = Chr(9) + "This is a test" + Chr(13)
确保在文本前面添加制表符(Unicode 值 9),并在文本后面添加硬换行符(Unicode 值 13)。
访问字符串的部分字符
Apache OpenOffice Basic 提供了四个返回部分字符串的函数:
- Left(MyString, Length)
- 返回 MyString 的前 Length 个字符。
- Right(MyString, Length)
- 返回 MyString 的最后 Length 个字符。
- Mid(MyString, Start, Length)
- 从 Start 位置开始返回 MyString 的前 Length 个字符。
- Len(MyString)
- 返回 MyString 中的字符数目。
下面是调用这些函数的几个示例:
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
搜索和替换
Apache OpenOffice Basic 提供了 InStr 函数,用于在另一个字符串中搜索部分字符串:
ResultString = InStr (MyString, SearchString)
SearchString 参数指定要在 MyString 中搜索的字符串。该函数返回一个数字,其中包含 SearchString 在 MyString 中首次出现的位置。如果要查找字符串的其他匹配项,还可以通过该函数指定 Apache OpenOffice Basic 开始搜索的可选起始位置。在这种情况下,该函数的语法为:
ResultString = InStr(StartPosition, MyString, SearchString)
在前面的示例中,InStr 忽略字符的大小写。要更改搜索以使 InStr 区分大小写,请添加参数 0,如以下示例中所示:
ResultString = InStr(MyString, SearchString, 0)
通过使用上面用于编辑字符串的函数,程序员可以在一个字符串中搜索并替换另一个字符串:
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
该函数通过 InStr 以循环方式在原始项 Source 中搜索传送的 Search 字符串。如果找到搜索项,则会提取表达式前面的部分,并将其写入到 Result 返回缓冲区中。它在搜索项 Search 所在的位置添加新的 Part 部分。如果找不到搜索项的其他匹配项,该函数将确定仍然保留的字符串部分,并将其添加到返回缓冲区中。函数返回以这种方式生成的字符串,作为替换过程结果。
由于替换部分字符序列是最常用的功能之一,因此,已扩展了 Apache OpenOffice Basic 中的 Mid 函数,以便自动执行此任务。以下示例将用字符串 is 替换 MyString 字符串中从第六个位置开始的三个字符。
Dim MyString As String
MyString = "This was my text"
Mid(MyString, 6, 3, "is")
设置字符串格式
Format 函数将数字的格式设置为字符串。为此,该函数需要指定一个 Format 表达式,然后将该表达式用作设置数字格式的模板。模板中的每个占位符可确保在输出值中相应地设置该项的格式。模板中的五个最重要的占位符是零 (0)、英镑符号 (#)、小数点号 (.)、逗号 (,) 和美元符号 ($) 字符。
模板中的 0 字符可确保始终在相应位置放置数字。如果没有提供数字,则会在其位置显示 0。
. 表示在特定于国家/地区的设置中由操作系统定义的小数点符号。
下面的示例说明了 0 和 . 字符如何在表达式中定义小数点后面的数字:
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"
同样,可以在数字前面添加零以达到所需的长度:
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"
, 表示操作系统使用的千位分隔符,而 # 表示一个数字或位置,仅当输入字符串需要时才会显示。
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"
Format 函数显示系统定义的相关货币符号以替代 $ 占位符(本示例假设已定义了欧洲语言环境):
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 €"
也可以使用 VBA 中用于设置日期和时间详细信息格式的 Format 指令:
sub main
dim myDate as date
myDate = "01/06/98"
TestStr = Format(myDate, "mm-dd-yyyy") ' 01-06-1998
MsgBox TestStr
end sub
Content on this page is licensed under the Public Documentation License (PDL). |