How do I check if a string contains a list of characters?
Solution 1
use regular expression in java to check using str.matches(regex_here)
regex in java
for example:
if("asdhAkldffl".matches(".*[ABCDEFGH].*"))
{
System.out.println("yes");
}
Solution 2
The cleanest way to implement this is using 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:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
Solution 3
From Guava: CharMatcher.matchesAnyOf
private static final CharMatcher CHARACTERS = CharMatcher.anyOf("ABCDEFGH");
assertTrue(CHARACTERS.matchesAnyOf("39823839A983923"));
Solution 4
If "ABCDEFGH"
is in a string variable, the regular expression solution is not good. It will not work if the string contains any character that has a special meaning in regular expressions. Instead I suggest:
Set<Character> charsToTestFor = "ABCDEFGH".chars()
.mapToObj(ch -> Character.valueOf((char) ch))
.collect(Collectors.toSet());
String stringToTest = "asdhAkldffl";
boolean anyCharInString = stringToTest.chars()
.anyMatch(ch -> charsToTestFor.contains(Character.valueOf((char) ch)));
System.out.println("Does " + stringToTest + " contain any of " + charsToTestFor + "? " + anyCharInString);
With the strings asdhAkldffl
and ABCDEFGH
this snippet outputs:
Does asdhAkldffl contain any of [A, B, C, D, E, F, G, H]? true
Solution 5
This is how it can be achieved using Pattern and Matcher,
Pattern p = Pattern.compile("[^A-Za-z0-9 ]");
Matcher m = p.matcher(trString);
boolean hasSpecialChars = m.find();
Related videos on Youtube
Rahul Tiwari
Mobile developer and enthusiast. Interested in Android, Kotlin, iOS, Java.
Updated on July 09, 2022Comments
-
Rahul Tiwari almost 2 years
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.
-
Alex Kreutznaer over 11 yearsDo you mean you want to check if some String contains characters from the list?
-
sage88 over 11 yearsShould probably look around a bit elsewhere before asking questions like this.
-
Mawia over 11 yearsI think OP means to ask regular expression but did not know how to ask
-
-
Pradeep Simha over 11 yearsThis is a inefficient solution, using regex is better.
-
EAKAE over 11 yearsI postedit because it is simpler and easier to understand if you don't know regex yet.
-
mre over 11 yearswhat about locale sensitivity?
-
Rana Ghosh almost 10 years@advocate Probably because it's not build into java. You have to download Apache Commons Lang to get it. commons.apache.org/proper/commons-lang Make sure to add it to your classpath.
-
fIwJlxSzApHEZIl almost 10 yearsThanks! For anyone else you need to unzip the download package (I recommend in your project folder). Right click your project in Eclipse -> Build Path -> Configure Build Path -> Add External Jars -> Select the commons lang jars. You will also need the right version number in your inport statement: import org.apache.commons.lang3.StringUtils;
-
Alex about 8 yearsYeah but good luck if you want to check if it contains only numeric data with this method.
-
Evren Ozturk about 6 yearsEven if it is. That is not an acceptable attitude to answer.
-
Vitali Tchalov about 6 yearsThis should be the accepted answer - it is cleaner, simpler and in my tests for verifying Windows file names for reserved characters using a pre-compiled RegEx (e.g. Pattern.compile(".*?[<>:\"/\\|?*]+?.*")), StringUtils.containsAny performed ~6 times faster.
-
JGFMK over 4 yearsThe question asked for a list of characters, not a single one
-
Rana Ghosh over 2 yearsI hadn't considered this when I gave my answer. This is a good point. There are ways to get around it. You could escape special characters with a "\". I'm not saying this as an "on the other hand," I'm just letting others know every angle.