检查字符串是否有特殊字符。如果是,请用空格替换它们,否则忽略 [英] check if a string has special characters..If yes replace them with space else ignore

查看:103
本文介绍了检查字符串是否有特殊字符。如果是,请用空格替换它们,否则忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是我已经完成的了......但我可以看到这不是一种有效的方式...



Hi,

This is wat i have done...But i can see this is not an efficient way...

For x = 126 To 255
      If strName.Contains(Chr(x)) Then
          strName = strName.Replace(Chr(x), " ")
          dr("Name") = strName

      Else
          dr("Name") = parts(0)
      End If
  Next





有人可以提出建议让我更好的方式。



谢谢。



Can someone suggest me a better way.

Thanks.

推荐答案





您可以使用正则表达式用空格替换特殊字符。在此示例中,所有不是az,AZ或0-9的字符都将替换为空格:

Hi,

You can use a regular expression to replace special characters with whitespace. In this example, all characters which aren''t a-z, A-Z or 0-9 will be replaced with whitespace:
Dim input As String = "Hello^world" ' change this into your input
Dim replaced As String = System.Text.RegularExpressions.Regex.Replace(input, "[^a-zA-Z0-9]", " ")
Console.WriteLine(replaced) ' output: Hello world



关于常规的更多信息表达式:

30分钟正则表达式教程 [ ^ ]

Regex.Replace(String,String,String)方法:

http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx [ ^ ]


你可以做的第一个改进是完全摆脱它。

如果字符串不包含char值255,那么dr(name)将始终包含部分(0)和(推测) )修改后的strName值将被丢弃 - 因为循环中的最终值决定了数组元素的值。

所以我认为这不是你想要做的...



你想要达到什么目的?

我问的原因是unicode字符(由字符串组成)不一定只在0..255范围内如果你只是在可打印字符之后那么它是相对的很简单 - 但我可能会使用正则表达式





我不想声明特殊字符。我不喜欢使用ch (x)其中x = 126到255 ...我想基本上检查一个字符串中的特殊字符。如果发现用空格替换它们我想将字符串存储在我的数据流博士中。



空间下方的特殊字符怎么样?你也不想摆脱它们吗?

我可以建议你从*允许的*字符列表开始,并使用正则表达式替换所有不在的字符它?这是一个更简单的解决方案:

The first improvement you could make is to get rid of that completely.
If the string does not contain the char value 255, then dr("name") will always contain parts(0) and (presumably) the revised strName value will be discarded - because the final value in the loop determines the value of the array element.
So I assume that is not what you are trying to do...

What are you trying to achieve?
The reason I ask is that unicode characters (which is what strings are made up of) are not necessarily just in the range 0..255 If you are after printable characters only then it''s relatively simple - but I would probably use a Regex


"I dont want to declare the special characters. I shud use ch(x) where x=126 to 255 ...I want to basically check for the special characters in a string. If found replace them with a space else i want to store the string in my datarow dr."

What about the special characters below space? Don''t you want to get rid of them too?
Can I suggest that you start with a list of *permitted* characters and use a regex to replace all the characters which aren''t on it? It''s a simpler solution:
Public Dim regex As Regex = New Regex("[^\w\.\,!""


%^& * \(\) - _ + = :: @'])


' 这是替换字符串
公共 Dim regexReplace As String =


' 替换InputText中匹配的文本使用替换模式
Dim 结果 As String = regex.Replace(InputText,regexReplace)
%^&*\(\)-_+=::@']") ' This is the replacement string Public Dim regexReplace As String = " " ' Replace the matched text in the InputText using the replacement pattern Dim result As String = regex.Replace(InputText,regexReplace)


这篇关于检查字符串是否有特殊字符。如果是,请用空格替换它们,否则忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆