How do I check if a string contains a list of characters?
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>
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");
}