简体   繁体   English

如何在Java中检查字符串中是否包含多个特定字符

[英]How can I check a string for multiple specific characters in java

I need to check a string to see if it contains any one of these characters (#, $, %, &) in java. 我需要检查一个字符串以查看它是否包含Java中的以下任意字符(#,$,%和&)。 I need to check for all 4 at the same time, with out a loop if possible and without regex. 我需要同时检查所有4个,如果可能且没有正则表达式,则不进行循环。 I am using this to verify validity of an email address so I cant just check for special characters as the email contains the @. 我正在使用它来验证电子邮件地址的有效性,所以我不能仅检查特殊字符,因为电子邮件中包含@。 Is there a way to use the .contains method with a switch statement? 有没有办法将.contains方法与switch语句一起使用?

Given a String str , and no loops or regular expressions you could presumable call String.contains(CharSequence) with a series of boolean ands like 给定一个String str ,没有循环或正则表达式,您可以假定使用一系列布尔值和类似的String来调用String.contains(CharSequence)

if (str.contains("#") && str.contains("$") && str.contains("%")
        && str.contains("&")) {
     System.out.printf("%s contains #$%%&%n", str);
} else {
     System.out.printf("%s does not contain #$%%&%n", str);
}

您应该使用String.contains(...)

String.subString(String foo) allows you to search for specific case sensitive characters. String.subString(String foo)允许您搜索特定的区分大小写的字符。

Foo.subString(String.ignoreCase("bar") will search foo for the string of characters "bar". Foo.subString(String.ignoreCase("bar")将在foo中搜索字符“ bar”的字符串。

This will return the index of the first letter of "bar", and return -1 if the string is not found. 这将返回“ bar”的第一个字母的索引,如果找不到该字符串,则返回-1。

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

相关问题 我可以检查字符串中除特定字符之外的其他字符吗? - Can I check a String for characters other than specific ones? 如何检查Java中另一个String的字符是否可以组成一个String? - How to check if a String can be formed from the characters of another String in Java? 如何使用 Java 多次转义字符串中的特定字符 - How to Escape Specific Characters Multiple Times in a String Using Java 如何检查特定字符串是否在另一个字符串中多次出现? - How can I check if a specific string occurs multiple times in an other string? 如何检查字符串中是否存在某些特定字符并在Java中删除这些字符 - How to check if some specific characters present in the string and remove those characters in java 如何检查字符串 [Java] 中的特殊字符? - How do I check for special characters in a String[Java]? 如何从Java中的特定字符串中删除特定字符? - How do I delete specific characters from a particular String in Java? 如何仅扫描四个特定字符的字符串? - How can I scan a string for only four specific characters? 如何在字符串中查找两个特定字符? - How can I look for two specific characters in a string? 如何阅读直到Java中特定字符为止的字符? - How can I read characters until a specific one in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM