语句print ("hello") 中双引号包含的多个字符,在FB语言中称为字符串。字符串也可以用字符型数组进行存储,输入并运行以下代码:
Sub 游戏执行过程(hWndForm As hWnd)
Dim str1 As String = "hello"
Print str1
Dim str2 As ZString * 5 = "hello"
Print str2
Dim str3 As WString * 5 = "中文五个字"
Print str3
End Sub
程序运行后输出:
hello
hell
中文五个
其中:
Dim str1 As String 是随意装任意长度的字符串,装多少显示多少
Dim str2 As ZString * 5 是定长字符串, 5 表示最多5个字符,为了于C语言兼容,最后一个是 0字符串,实际可以装 4个字符串
`Dim str3 As WString 5` 是Unicode编码 字符串,简单的说,ZString 是ASCII 编码,一个英文占1个空间,一个中文占2个字符空间,而 WString 不管中文英文,都占1个空间。
要存储多个字符串,也可以应用字符型二维数组。
Sub 游戏执行过程(hWndForm As hWnd)
Dim str1(2) As String = {"Wuhan","Beijing","Shanghai"}
Dim i As Long
'通过二维数组元素输出
For i = 0 To 2
Print str1(i)
Next
End Sub
程序运行后输出:
Wuhan
Beijing
Shanghai
提示
字符在内存中实际存储的也是一个整数值,该整数值称为该字符的ASCII码。读者可以尝试输入并运行以下代码:
Dim str1 As String ="Wuhan"
print str1[0],str1[1],str1[2],str1[3],str1[4]
程序运行后输出:87 | 117 | 104 | 97 | 110
可以网上搜索ASCII码,W=87 u=117 h=104 a=97 n=110
为了处理中文等复杂字符,Unicode编码 的 WString
Sub 游戏执行过程(hWndForm As hWnd)
Dim str1 As ZString *10 = "Wu中文"
Dim str2 As WString *10 = "Wu中文"
Print str1[0], str1[1], str1[2], str1[3], str1[4], str1[5]
Print str2[0], str2[1], str2[2], str2[3]
End Sub
程序运行后输出:
87 | 117 | 214 | 208 | 206 | 196
87 | 117 | 20013 | 25991
ZString 是ASCII码,一个中文是2个字符,WString 是Unicode编码,一个中文是1个字符
ZString WString 必须先指定 n容量,然后最多装指定的 n容量 -1 的字符。
String 是变长字符,可以装任何长度,当然内存有限,实际装 1.8G字符软件就崩溃。
String 实际是数据容器,默认 ASCII码字符串,可以装任何内容。
评论一下?