vbs字符串操作小考题,学习的朋友可以参考下。
str="a|b|c|d"
要求写一段小程序得出:
a|b|c|d
b|c|d|a
c|d|a|b
d|a|b|c
这样的结果,str长度未定,格式是上边的格式。
实现代码如下:
代码如下:
str="a|b|c|d"
Call Sort(str, "|")
Function Sort(sSource, sDelimiter)
Dim I, J, N, sItems, sTemp
sItems = Split(sSource, sDelimiter)
For I = 0 To UBound(sItems)
For J = 0 To UBound(sItems)
N = I + J
If N > UBound(sItems) Then
N = N - UBound(sItems) - 1
End If
sTemp = sTemp & sItems(N) & sDelimiter
Next
sTemp = Left(sTemp, Len(sTemp) - Len(sDelimiter))
Wscript.Echo sTemp
sTemp = ""
Next
End Function
在一个窗口同时显示
代码如下:
str = "a|b|c|d"
WSH.Echo Join(Sort(str, "|"), vbCrLf)
Function Sort(ByVal s, ByVal d)
Dim a, r(), i, j, h, index
a = Split(s, d)
h = UBound(a)
ReDim Preserve r(h)
index = Len(d) + 1
For i = 0 To h
r(i) = ""
For j = i To h + i
r(i) = r(i) & d & a(j Mod (h + 1))
Next
r(i) = Mid(r(i), index)
Next
Sort = r
End Function
代码如下:
str="a|b|c|d"
Wscript.Echo Sort(str, "|")
Function Sort(sSource, sDelimiter)
Dim I, J, N, sItems, sTemp
sItems = Split(sSource, sDelimiter)
N = UBound(sItems)
For I = 0 To N
For J = 0 To N
sTemp = sTemp & sItems((I + J) Mod (N + 1)) & sDelimiter
Next
sTemp = Left(sTemp, Len(sTemp) - Len(sDelimiter))
Sort = Sort & sTemp & vbCrLf
sTemp = ""
Next
Sort = Left(Sort, Len(Sort) - 1)
End Function
代码如下:
str="a|b|c|d"
msgbox Sort(str, "|")
function sort(ss,sd)
dim n,i
for i=0 to ubound(split(ss,sd))
sort=sort+mid(ss+sd+ss,n+1,len(ss))+vbcrlf
n=instr(n+1,ss+sd+ss,sd)
next
end function