user1989216
user1989216

Reputation:

How do I check if a string contains any characters from a given set of characters represented in another string?

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.

Upvotes: 26

Views: 89009

Answers (7)

Daniel Kaplan
Daniel Kaplan

Reputation: 67370

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>

Upvotes: 30

Anonymous
Anonymous

Reputation: 86276

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

Upvotes: 3

KayV
KayV

Reputation: 13845

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();

Upvotes: 1

Neil
Neil

Reputation: 4049

From Guava: CharMatcher.matchesAnyOf

private static final CharMatcher CHARACTERS = CharMatcher.anyOf("ABCDEFGH");
assertTrue(CHARACTERS.matchesAnyOf("39823839A983923"));

Upvotes: 4

EAKAE
EAKAE

Reputation: 153

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.

Upvotes: -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.

Upvotes: 1

vishal_aim
vishal_aim

Reputation: 7854

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");
    }

Upvotes: 44

Related Questions