-2

I have got a String array and I want to check various subsections of the array whether they contain any and not all of the given characters in the CharSequence

For example

CharSequence obj = "12";
//convert section to string
String s = Arrays.asList(arr[1]).subList(0,2).toString();
if (s.contains(obj))
{
    System.out.print("yes");
}

This prints out yes only when both 1 and 2 are to be found in s but I want to check whether either 1 or 2 are there?

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
user3758223
  • 7
  • 1
  • 7

5 Answers5

1

Use your chars in a regex:

CharSequence obj = "12";
String s = Arrays.asList(arr[1]).subList(0,2).toString();
if (s.matches(".*[" + obj + "].*")) {
    // either "1" or "2" is in s
}

FYI, in java matches() must match the whole string to return true - that's why the .* is on either end of the regex.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Be aware this is slow due to the parsing required in the regex. In cases where performance matters you want to use indexOf. – WestCoastProjects Jun 28 '14 at 21:15
  • @javadba the "performance probkem" would be negligible (a few micro seconds). Simple, readable, brief code is preferable to verbose, complicated but fast code, unless you actually can prove there's a performance problem that needs fixing – Bohemian Jun 29 '14 at 04:49
  • @Bohemian Suit yourself - earlier this year I reduced the runtime of a critical high volume app by 75% just by being more careful with string manipulation items and the first thing that had to go were the split()'s, Pattern's etc anything that required a formal parser. – WestCoastProjects Jun 29 '14 at 05:47
0
CharSequence obj = "12";
      //convert section to string
      String s = Arrays.asList(arr[1]).subList(0,2).toString();

    for(int i = 0;i<obj.lenth();i++){
      if (s.indexof(obj.charAt(i)) != -1)
        {
            System.out.print("yes");
        }
    }
samir
  • 4,501
  • 6
  • 49
  • 76
0

I believe the solution to your problem is similar as to what was asked here: In Java, how can I determine if a char array contains a particular character?

That question had a few interesting solutions offered: 1) you can use indexOf, or 2) you can test for the conditions in which what you're looking for ISN'T in the array.

Community
  • 1
  • 1
xzybit11
  • 26
  • 2
0

Consider the method containsAny shown below.

public class InTest {   
    public static boolean containsAny(String strToSearch, CharSequence chars) {
        for (int i=0;i<chars.length(); i++) {
            if (strToSearch.indexOf(chars.charAt(i)) >= 0) {
                return true;
            }
        }
        return false;

    }
    public static void main(String[] args) {
        System.out.println(containsAny(args[0],args[1]));
    }
}

Try it out:

C:\apps\simpleakka>javac InTest.java

C:\apps\simpleakka>java InTest "abcdefghij" "i"
true

C:\apps\simpleakka>java InTest "abcdefghij" "n"
false
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
0

Can't you just:

boolean found = false;
for(int i = 0, i < obj.length() && !found; i++)
{
    if(s.contains(obj.subSequence(i, i + 1)))
    {
        found = true;
    }
}
if(found)
{
    System.out.print("yes");
}
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75