我试图去除除字母和空格之外的所有字符,但我无法这样做。我目前拥有的代码如下,我怎么能改变它,所以它允许空格?目前它需要文本,剥离它,它都变成了一大行文本。
Dim InputTxt As String = InputText.Text
Dim OutputTxt As System.Text.StringBuilder = New System.Text.StringBuilder()
For Each Ch As Char In InputTxt
If (Not Char.IsLetter(Ch)) Then
OutputTxt.Append(Ch)
Continue For
End If
Dim CheckIndex As Integer = Asc("a") - (Char.IsUpper(Ch) * -32)
Dim Index As Integer = ((Asc(Ch) - CheckIndex) + 13) Mod 26
OutputTxt.Append(Chr(Index + CheckIndex))
Next
OutputText.Text = (OutputTxt.ToString())
答案 0 :(得分:3)
Dim output = New StringBuilder()
For Each ch As Char In InputTxt
If Char.IsLetter(ch) OrElse ch = " " Then
output.Append(ch)
End If
Next
OutputText.Text = output.ToString()
答案 1 :(得分:1)
尚未完全测试,但简单的正则表达式应该覆盖您的所有代码
Dim s = "ADB,12.@,,,122abC"
Dim result = Regex.Replace(s, "[^a-zA-Z ]+", "")
Console.WriteLine(result)
- > output = ADBabC
答案 2 :(得分:0)
这是一种使用LINQ查询字符串的方法。
Dim candidateText = "This is a test. Does it work with 123 and !"
Dim q = From c In candidateText
Where Char.IsLetter(c) OrElse c=" "
Select c
candidateText = String.Join("", q.ToArray)
修改强>
删除查询中的Char.IsWhiteSpace以匹配OP问题。
答案 3 :(得分:0)
我认为graumanoz解决方案是最好的,并且不会使用任何不必要的操作,例如ToList
,但只是为了踢:
Shared Function Strip(ByVal input As String)
Dim output = New StringBuilder()
input.ToList().Where(Function(x) Char.IsLetter(x) OrElse x = " ").ToList().
ForEach(Function(x) output.Append(x))
Return output.ToString()
End Function