我只需要使用Arrays和Split()函数,而不是String.Spit()。
这是我到目前为止所做的,它不断印刷相同的东西。
Function RemoveS(sta As String) As String
Dim Warray() As String
Dim sp As String = " "
Dim EndStr As String = ""
Warray = Split(sta, sp)
For i = 0 To UBound(Warray)
EndStr += Warray(i) + " "
Next i
Return EndStr
End Function
例如:
Dim A = "This is a wrong String"
RemoveS(A)
应输出"This is a wrong String"
答案 0 :(得分:0)
这是一种使用Split()
,Join()
和LINQ:
Dim wrongString As String = "This Is a wrong String"
Dim correctedString = RemoveContigousWhiteSpace(wrongString)
Function RemoveContigousWhiteSpace(str As String) As String
Return String.Join(" ", Split(str, " ").
Where(Function(s) Not String.IsNullOrWhiteSpace(s)))
End Function
你提到你只能使用数组和Split()
- 函数,所以这是另一种方法For
- 循环:
Function RemoveContigousWhiteSpace(str As String) As String
Dim strArr As String() = Split(str, " ")
Dim corrString As String = ""
For i As Integer = 0 To strArr.Length - 1
If Not String.IsNullOrWhiteSpace(strArr(i)) Then
If i = 0 Then
corrString = strArr(i)
Else
corrString &= " " & strArr(i)
End If
End If
Next
Return corrString
End Function