简体   繁体   English

如何检查一个字符串上多少个非字母数字字符

[英]How to check how many non-alphanumeric characters on a string

i'm trying to check how many non-alphanumeric characters can be found on a string for example: 我正在尝试检查在一个字符串上可以找到多少个非字母数字字符,例如:

String message = "i'm a keyboard!!!"

i want to check how many "!" 我想检查多少个“!” and " ' " can be found on that string, not only those, but any symbol. 和“”不仅可以在字符串上找到,而且可以在任何符号上找到。

Regular expressions would be good for this. 正则表达式将对此有所帮助。 Try just replacing alphanumeric character with nothing and taking the length. 尝试仅将字母数字字符替换为空并获取长度。

pattern = "[a-zA-Z0-9]*"
newString = message.replaceAll(pattern, "")
return newString.length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM