[英]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.