如何检查字符串是否包含特殊字符(!@#$%^& *()_ +) [英] How to check if a string contains a special character (!@#$%^&*()_+)
本文介绍了如何检查字符串是否包含特殊字符(!@#$%^& *()_ +)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道什么是检查字符串是否为
I was wondering what would be the best way to check if a string as
$str
包含以下任何字符
!@#$%^&*()_+
我曾考虑使用ASCII值,但是对于确切的实现方式却有些困惑.
I thought of using ASCII values but was a little confused on exactly how that would be implemented.
或者,如果有一种更简单的方法可以根据值检查字符串.
Or if there is a simpler way to just check the string against the values.
推荐答案
将其与全局匹配.您只需要转义外壳否则认为特殊的字符:
Match it against a glob. You just have to escape the characters that the shell otherwise considers special:
#!/bin/bash
str='some text with @ in it'
if [[ $str == *['!'@#\$%^\&*()_+]* ]]
then
echo "It contains one of those"
fi
这篇关于如何检查字符串是否包含特殊字符(!@#$%^& *()_ +)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文