23

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.

starball
  • 20,030
  • 7
  • 43
  • 238

7 Answers7

43

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");
    }
vishal_aim
  • 7,636
  • 1
  • 20
  • 23
28

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>
f_puras
  • 2,521
  • 4
  • 33
  • 38
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 2
    @advocate Probably because it's not build into java. You have to download Apache Commons Lang to get it. http://commons.apache.org/proper/commons-lang/ Make sure to add it to your classpath. – Daniel Kaplan Jul 23 '14 at 22:31
  • Thanks! 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; – fIwJlxSzApHEZIl Jul 24 '14 at 02:12
  • 4
    This 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. – Vitali Tchalov Feb 23 '18 at 20:52
4

From Guava: CharMatcher.matchesAnyOf

private static final CharMatcher CHARACTERS = CharMatcher.anyOf("ABCDEFGH");
assertTrue(CHARACTERS.matchesAnyOf("39823839A983923"));
Neil
  • 3,899
  • 1
  • 29
  • 25
3

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I 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. – Daniel Kaplan Jan 23 '22 at 01:41
1

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.

1

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();
KayV
  • 12,987
  • 11
  • 98
  • 148
-2

This seems like a Homework question... -_-

You can use the String.contains() function.
For example:

"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.

EAKAE
  • 153
  • 1
  • 11