简体   繁体   English

如何检查一个字符串是否至少包含另一个字符串中的一个字符?

[英]How do I check if a string contains at least one character from another string?

I'm using Java 6. I have a string that contains "special" characters -- "!@#$%^&*()_". 我正在使用Java6。我有一个包含“特殊”字符的字符串-“!@#$%^&*()_”。 How do I write a Java expression to check if another string, "password", contains at least one of the characters defined in the first string? 如何编写Java表达式以检查另一个字符串“ password”是否至少包含第一个字符串中定义的字符之一? I have 我有

regForm.getPassword().matches(".*[\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+].*")

but I don't want to hard-code the special characters, rather load them into a string from a properties file. 但是我不想对特殊字符进行硬编码,而是将它们从属性文件加载到字符串中。 So I'm having trouble figuring out how to escape everything properly after I load it from the file. 因此,在从文件中加载所有内容后,我很难弄清楚如何正确地转义所有内容。

You can try creating regex from string that contains special characters and escape symbols using Pattern.quote . 您可以尝试使用Pattern.quote从包含特殊字符和转义符号的字符串中创建正则表达式。 Try this: 尝试这个:

String special = "!@#$%^&*()_";
String pattern = ".*[" + Pattern.quote(special) + "].*";
regFrom.getPassword().matches(pattern);

I think simple looping the regex to check each character might work better and will work for all the cases : 我认为简单循环正则表达式以检查每个字符可能会更好,并且在所有情况下都适用

String special = "!@#$%^&*()_";
boolean found = false;
for (int i=0; i<special.length(); i++) {
   if (regFrom.getPassword().indexOf(special.charAt(i)) > 0) {
      found = true;
      break;
   }
}
if (found) { // password has one of the allowed characters
   //...
   //...
}

One option is to use StringTokenizer , and see if it returns more than 1 substring. 一种选择是使用StringTokenizer ,看看它是否返回多个子字符串。 It has a constructor that allows specifying the characters to split by. 它有一个构造函数,允许指定要分割的字符。

Anyway, my favourite option would be just iterating the characters and using String.indexOf . 无论如何,我最喜欢的选项是迭代字符并使用String.indexOf

暂无
暂无

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

相关问题 如何检查 Java 字符串是否至少包含一个大写字母、小写字母和数字? - How do I check if a Java String contains at least one capital letter, lowercase letter, and number? java中如何判断一个字符串是否至少包含一个字母? - How to check whether a string contains at least one alphabet in java? 如何检查一个树集至少包含另一个树集中的一个项目 - How to check a treeset contains at least one items from another treeset 如何使用正则表达式查找字符串是否至少包含一个字符? - How do I find if string has at least one character using regex? 如何检查字符串是否包含关键字? - How do I check if a string contains keywords? 如何检查仅包含字母(大写和小写)以及至少一个数字的字符串 - How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only 如何确定一个字符串矩形的每个边框是否至少包含一个指定字符? - how to determine whether each border side of a string rectangle contains at least one specified character? 有没有办法用java递归检查字符串是否包含至少一个大写字母? - Is there a way to check if a string contains at least one capital letter recursively with java? 使用正则表达式检查一个字符串至少包含一个Unicode字母 - Check a string contains at least one Unicode letter using regex 字符串至少包含一位数字 - String contains at least one digit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM