FreeBasic WString 关键字
最后修改日期:2025 年 6 月 16 日
FreeBasic 的 WString 关键字代表一个宽字符串数据类型,它可以存储 Unicode 字符。WString 对于处理超出 ASCII 的国际文本和符号至关重要。
基本定义
在 FreeBasic 中,WString 是一个内置的 Unicode 字符串数据类型。每个字符占用 2 个字节(UTF-16 编码),能够表示各种语言和符号的字符。
WString 变量像常规字符串一样是空终止的,但可以处理扩展字符集。它们对于国际化以及在使用需要宽字符串的 Windows API 函数时特别有用。
声明 WString 变量
此示例演示了如何声明和初始化 WString 变量。
Dim greeting As WString * 50 = WStr("Hello, 世界!")
Dim emptyWString As WString
Print greeting
Print Len(greeting) ' Returns 9 (8 characters + null terminator)
Print emptyWString ' Prints nothing
这里我们声明了一个具有 50 个字符空间的固定长度 WString,并用 Unicode 字符串对其进行初始化。WStr 函数将字符串字面量转换为 WString 格式。请注意 Len 返回的是字节长度。
带 Unicode 字符的 WString
WString 可以存储来自各种书写系统和符号的字符。
Dim text As WString = WStr("FreeBasic supports: ")
text += WStr("日本語, Русский, Ελληνικά, العربية")
Print text
For i As Integer = 0 To Len(text) - 1
Print Hex(AscW(text[i])); " ";
Next
此示例演示了 WString 处理多种脚本的能力。我们使用 += 连接字符串,并打印每个字符的 Unicode 代码点。AscW 函数返回 Unicode 值。
在 String 和 WString 之间转换
FreeBasic 提供了在 String 和 WString 类型之间进行转换的函数。
Dim asciiText As String = "ASCII text"
Dim wideText As WString = WStr("Wide text: 测试")
' Convert String to WString
Dim convertedWide As WString = WStr(asciiText)
' Convert WString to String
Dim convertedAscii As String = Str(wideText)
Print convertedWide
Print convertedAscii
Print Len(convertedAscii) ' Note: May lose information
这展示了字符串类型之间的转换。WStr 从 String 转换为 WString,而 Str 则转换回来。请注意,转换为 String 可能会丢失当前代码页中不存在的 Unicode 字符。
带 Windows API 的 WString
在调用 Windows API 函数时,WString 特别有用。
#Include Once "windows.bi"
Declare Function MessageBoxW Lib "user32" _
(ByVal hWnd As Integer, ByVal lpText As WString, _
ByVal lpCaption As WString, ByVal uType As Integer) As Integer
Dim title As WString = WStr("WString Demo")
Dim message As WString = WStr("This is a Unicode message: ☺")
MessageBoxW(0, message, title, 0)
此示例演示了如何将 WString 与 Windows API 一起使用。我们声明了 MessageBox 的 Unicode 版本(MessageBoxW),它需要 WString 参数。该消息包含一个 Unicode 笑脸字符,而使用 String 则无法正常工作。
WString 操作
WString 支持与常规 String 许多相同的操作。
Dim ws1 As WString = WStr("FreeBasic ")
Dim ws2 As WString = WStr("WString操作")
' Concatenation
Dim combined As WString = ws1 + ws2
' Comparison
If ws1 < ws2 Then
Print "ws1 comes first"
End If
' Substring
Print Mid(combined, 10, 3) ' Prints "WSt"
' Searching
Print InStr(combined, WStr("操作")) ' Prints position
这展示了 WString 的常见字符串操作。请注意,比较是基于 Unicode 代码点值的。Mid 和 InStr 的工作方式与它们的 String 对应项类似,但能正确处理宽字符。
WString 数组
WString 数组对于存储多个 Unicode 字符串很有用。
Dim languages(3) As WString
languages(0) = WStr("English")
languages(1) = WStr("Español")
languages(2) = WStr("Français")
languages(3) = WStr("日本語")
For i As Integer = 0 To 3
Print languages(i)
Next
此示例创建了一个 WString 数组,用于以其原生脚本存储语言名称。每个元素都可以包含 Unicode 字符,并且我们可以像处理任何其他数组一样处理它们。
WString 和文件 I/O
读取和写入 WString 到文件需要特别注意。
Dim filename As String = "unicode.txt"
Dim content As WString = WStr("File content: ❤☀☆☂☻♞")
' Write UTF-16 (little-endian) with BOM
Open filename For Binary Access Write As #1
Put #1, , WChr(&hFEFF) ' Write UTF-16 BOM
Put #1, , content
Close #1
' Read back
Dim readContent As WString
Open filename For Binary Access Read As #1
Seek #1, 3 ' Skip BOM (2 bytes) + null terminator
Get #1, , readContent
Close #1
Print readContent
此示例演示了使用 UTF-16 编码将 WString 读取和写入文件。我们包含了 BOM(字节顺序标记)以进行正确的编码标识。请注意在读取时字节位置的谨慎处理。
最佳实践
- 使用 WString 处理 Unicode 文本或 Windows API
- 保持一致应用程序中的字符串类型
- 谨慎处理转换以避免数据丢失
- 考虑编码读写文件时
- 使用 WStr 处理 WString 字面量
本教程通过实际示例介绍了 FreeBasic 的 WString 关键字,展示了它在不同场景下的用法。