VB中 对输入的字符串用asc码由从小到大排序
发布网友
发布时间:2022-08-08 15:00
我来回答
共3个回答
热心网友
时间:2024-11-24 19:15
Private Sub Form_Click()
Dim arr() As String
s = InputBox("输入你的字符串")
Print "源字符串:" + vbCrLf + s + vbCrLf
n = Len(s)
ReDim arr(n) As String
For i = 1 To n
arr(i) = Mid(s, i, 1)
Next
For i = 1 To n
For j = 1 To i
If Asc(arr(i)) < Asc(arr(j)) Then
tmp = arr(i): arr(i) = arr(j): arr(j) = tmp
End If
Next j, i
Print "排序后的新字符串:"
For i = 1 To n
Print arr(i);
Next
End Sub
热心网友
时间:2024-11-24 19:16
Private Sub Command1_Click()
Dim s As String, i%, t%, n%, f As String, j%, a%()
s = InputBox("请输入您要排序的字符串:", "提示信息")
n = Len(CStr(s))
ReDim a(n)
For i = 1 To n
a(i) = Asc(Mid(s, i, 1))
Next i
For i = 1 To n - 1
For j = i + 1 To n
If a(i) > a(j) Then
t = a(j)
a(j) = a(i)
a(i) = t
End If
Next j
Next i
For i = 1 To n
f = f & Chr(a(i))
Next i
Print f
End Sub
热心网友
时间:2024-11-24 19:16
如果我记得没错的话vb在比较数字和字母,字母和字母的大小的时候是自动比较ASCII 码的 ......
所以 放进数组直接用数组排序就可以解决