VB 中字符串长度测试问题

我有一个字母"G"和一个在智能ABC下输入的符号"♀",用len函数测试它们的长度都是1,但在textbox中输出,符号"♀"却占了两个位置,这样给我在文本对齐上带来了很大的麻烦,请问有什么办法解决?我写的代码如下:
Private Sub Command1_Click()
Dim a(1 To 1000) As String
Dim s1 As String
Dim s As String
Dim s2 As String
a(1) = "a": a(2) = "b": a(3) = "c": a(4) = "d": a(5) = "e"
a(6) = "f": a(7) = "g": a(8) = "h": a(9) = "i": a(10) = "j"
a(11) = "k": a(12) = "l": a(13) = "m": a(14) = "n": a(15) = "o"
a(16) = "p": a(17) = "q": a(18) = "r": a(19) = "s": a(20) = "t"
a(21) = "u": a(22) = "v": a(23) = "w": a(24) = "x": a(25) = "y"
a(26) = "z": a(27) = "A": a(28) = "B": a(29) = "C": a(30) = "D"
a(31) = "E": a(32) = "F": a(33) = "G": a(34) = "H": a(35) = "I"
a(36) = "J": a(37) = "K": a(38) = "L": a(39) = "M": a(40) = "N"
a(41) = "O": a(42) = "P": a(43) = "Q": a(44) = "R": a(45) = "S"
a(46) = "T": a(47) = "U": a(48) = "V": a(49) = "W": a(50) = "X"
a(51) = "Y": a(52) = "Z": a(53) = "〃": a(54) = "々": a(55) = "∈"
a(56) = "√": a(57) = "⊙": a(58) = "∫": a(59) = "∮": a(60) = "∞"
a(61) = "♂": a(62) = "♀": a(63) = "$": a(64) = "¤": a(65) = "§"
Randomize
X = Int(Rnd * 65 + 1)
For i = 1 To 1000
Randomize
If i Mod 9 = 0 Then
s1 = i & Space(1) & a(X)
s2 = s1 & Space$(12 - Len(s1))
s = s & s2
Else
s1 = i & Space(1) & a(Int(Rnd * 65 + 1))
s2 = s1 & Space(12 - Len(s1))
s = s & s2
End If
If i Mod 6 = 0 Then s = s & vbCrLf

Next i

Text1.Text = s
End Sub
最新回答
青春很沉默

2021-10-14 01:30:03

LenB("G")的值也为2哦......

可以用StrConv判断一下,等于0的话加空格,等于1的不处理

a = StrConv("♀", vbFromUnicode)
Print Len(a)

a=1

如果是StrConv("G", vbFromUnicode)的话,a=0
沧古烟

2023-01-29 06:26:18

加空格 然后再截取

比如 所有字符中最长是2位 那就每个字符都加三个空格 加完以后取前三位
裙身

2021-03-31 03:54:36

用lenb("♀"),值为2
初心不负

2023-11-05 05:42:35

改了一下,试过了没问题。

Private Sub Command1_Click()
Dim a(1 To 1000) As String
Dim s1 As String
Dim s As String
Dim s2 As String
Dim T As Integer, X As Integer
a(1) = "a": a(2) = "b": a(3) = "c": a(4) = "d": a(5) = "e"
a(6) = "f": a(7) = "g": a(8) = "h": a(9) = "i": a(10) = "j"
a(11) = "k": a(12) = "l": a(13) = "m": a(14) = "n": a(15) = "o"
a(16) = "p": a(17) = "q": a(18) = "r": a(19) = "s": a(20) = "t"
a(21) = "u": a(22) = "v": a(23) = "w": a(24) = "x": a(25) = "y"
a(26) = "z": a(27) = "A": a(28) = "B": a(29) = "C": a(30) = "D"
a(31) = "E": a(32) = "F": a(33) = "G": a(34) = "H": a(35) = "I"
a(36) = "J": a(37) = "K": a(38) = "L": a(39) = "M": a(40) = "N"
a(41) = "O": a(42) = "P": a(43) = "Q": a(44) = "R": a(45) = "S"
a(46) = "T": a(47) = "U": a(48) = "V": a(49) = "W": a(50) = "X"
a(51) = "Y": a(52) = "Z": a(53) = "〃": a(54) = "々": a(55) = "∈"
a(56) = "√": a(57) = "⊙": a(58) = "∫": a(59) = "∮": a(60) = "∞"
a(61) = "♂": a(62) = "♀": a(63) = "$": a(64) = "¤": a(65) = "§"

Randomize
X = Int(Rnd * 65 + 1)

For i = 1 To 1000
If i Mod 9 = 0 Then
If X < 53 Then
s1 = i & Space(1) & a(X)
s2 = s1 & Space(12 - Len(s1))
s = s & s2
Else
s1 = i & Space(1) & a(X)
s2 = s1 & Space(11 - Len(s1))
s = s & s2
End If
Else
Randomize
T = Int(Rnd * 65 + 1)
If T < 53 Then
s1 = i & Space(1) & a(T)
s2 = s1 & Space(12 - Len(s1))
s = s & s2
Else
s1 = i & Space(1) & a(T)
s2 = s1 & Space(11 - Len(s1))
s = s & s2
End If
End If

If i Mod 6 = 0 Then s = s & vbCrLf

Next i

Text1.Text = s
End Sub