How do I check if a list of characters are in a String, for example "ABCDEFGH" how do I check if any one of those is in a string.
如何检查字符列表是否在字符串中,例如“ABCDEFGH”如何检查其中任何一个是否在字符串中。
5 个解决方案
#1
16
use regular expression in java to check using str.matches(regex_here)
regex in java
在java中使用正则表达式来检查java中的str.matches(regex_here)regex
for example:
例如:
if("asdhAkldffl".matches(".*[ABCDEFGH].*"))
{
System.out.println("yes");
}
#2
10
The cleanest way to implement this is using StringUtils.containsAny(String, String)
实现此目的最干净的方法是使用StringUtils.containsAny(String,String)
package com.sandbox;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class SandboxTest {
@Test
public void testQuestionInput() {
assertTrue(StringUtils.containsAny("39823839A983923", "ABCDEFGH"));
assertTrue(StringUtils.containsAny("A", "ABCDEFGH"));
assertTrue(StringUtils.containsAny("ABCDEFGH", "ABCDEFGH"));
assertTrue(StringUtils.containsAny("AB", "ABCDEFGH"));
assertFalse(StringUtils.containsAny("39823839983923", "ABCDEFGH"));
assertFalse(StringUtils.containsAny("", "ABCDEFGH"));
}
}
Maven dependency:
Maven依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
#3
2
I think this is a newbie question, so i will give you the easies method i can think of: using indexof complex version include regex you can try if you want.
我认为这是一个新手问题,所以我会给你一个我能想到的easies方法:使用indexof复杂版本包括正则表达式你可以试试,如果你想。
#4
1
From Guava: CharMatcher.matchesAnyOf
来自Guava:CharMatcher.matchesAnyOf
private static final CharMatcher CHARACTERS = CharMatcher.anyOf("ABCDEFGH");
assertTrue(CHARACTERS.matchesAnyOf("39823839A983923"));
#5
0
This seems like a Homework question... -_-
这似乎是一个家庭作业问题... -_-
You can use the String.contains() function.
For example:
您可以使用String.contains()函数。例如:
"string".contains("a");
String str = "wasd";
str.contains("a");
but you will need to call it once per every character you want to check for.
但是你需要为每个要检查的角色调用一次。
#1
16
use regular expression in java to check using str.matches(regex_here)
regex in java
在java中使用正则表达式来检查java中的str.matches(regex_here)regex
for example:
例如:
if("asdhAkldffl".matches(".*[ABCDEFGH].*"))
{
System.out.println("yes");
}
#2
10
The cleanest way to implement this is using StringUtils.containsAny(String, String)
实现此目的最干净的方法是使用StringUtils.containsAny(String,String)
package com.sandbox;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class SandboxTest {
@Test
public void testQuestionInput() {
assertTrue(StringUtils.containsAny("39823839A983923", "ABCDEFGH"));
assertTrue(StringUtils.containsAny("A", "ABCDEFGH"));
assertTrue(StringUtils.containsAny("ABCDEFGH", "ABCDEFGH"));
assertTrue(StringUtils.containsAny("AB", "ABCDEFGH"));
assertFalse(StringUtils.containsAny("39823839983923", "ABCDEFGH"));
assertFalse(StringUtils.containsAny("", "ABCDEFGH"));
}
}
Maven dependency:
Maven依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
#3
2
I think this is a newbie question, so i will give you the easies method i can think of: using indexof complex version include regex you can try if you want.
我认为这是一个新手问题,所以我会给你一个我能想到的easies方法:使用indexof复杂版本包括正则表达式你可以试试,如果你想。
#4
1
From Guava: CharMatcher.matchesAnyOf
来自Guava:CharMatcher.matchesAnyOf
private static final CharMatcher CHARACTERS = CharMatcher.anyOf("ABCDEFGH");
assertTrue(CHARACTERS.matchesAnyOf("39823839A983923"));
#5
0
This seems like a Homework question... -_-
这似乎是一个家庭作业问题... -_-
You can use the String.contains() function.
For example:
您可以使用String.contains()函数。例如:
"string".contains("a");
String str = "wasd";
str.contains("a");
but you will need to call it once per every character you want to check for.
但是你需要为每个要检查的角色调用一次。