2

I have two strings str, str2. Both strings have spaces. I used contains method but it does not fit here because it checks only the exact characters. How to check whether one string is a substring or not here.

I have implemented it using the below code. So the function in the code prints true if String "FOO BAR" is present in the original String.

public static void main(String[] args) {
        String str = "FOO BAR";
        String str1 = "FOO T BAR";
        String orignialString;
        String stringToMatch;
        if (str1.length() > str.length()) {
            orignialString = str1;
            stringToMatch = str;
        } else {
            orignialString = str1;
            stringToMatch = str;
        }
        boolean flag = false;
        String[] stringarr = stringToMatch.split(" ");
        for (int i = 0; i < stringarr.length; i++) {
            if (orignialString.contains(stringarr[i])) {
                flag = true;
            } else {
                flag = false;
                break;
            }
        }
        System.out.println("IS SUBSTRING---> " + flag);            
    }

I'm looking out for more clean short code using Pattern Matcher / REGEX.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Vaibs
  • 1,546
  • 9
  • 31
  • See [Removing whitespace from strings in Java](https://stackoverflow.com/questions/5455794/removing-whitespace-from-strings-in-java). – Wiktor Stribiżew Aug 23 '17 at 10:31
  • Regexp will not tell you if one string is inluded into another. It would rather tell if a string matches some rule. If str in your example is dynamically changed, regexp will not help, otherwise it could. – Alexey R. Aug 23 '17 at 10:33
  • [How can I perform a partial match with java.util.regex.*?](https://stackoverflow.com/questions/2469231/how-can-i-perform-a-partial-match-with-java-util-regex) The first two answers might be what you are looking for? – Anna P. Aug 23 '17 at 10:39
  • @AnnaP. I gone through the link. My requirement is to check if substring is present in original String or not.In link example it finds only partial string – Vaibs Aug 23 '17 at 11:33

1 Answers1

3

This answer assumes that you want to identify FOO BAR as a substring of FOO T BAR because all the words of the former are also present as words in the latter. Using this logic, we can just form lists of words from each of the two strings and then iterate to check that all words are present.

boolean isSub = true;
String str = "FOO T BAR";
String str1 = "FOO BAR";
List<String> strList = Arrays.asList(str.split("\\s+"));
List<String> strSubList = Arrays.asList(str1.split("\\s+"));
for (String sub : strSubList) {
    if (!strList.contains(sub)) {
        isSub = false;
    }
}

System.out.println("input is a substring: " + isSub);

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Your code is running irrespective of length and looking at the problem statement, length plays a role. Likewise, your code fails if I put str1 as ' FOO TAN BAR' & str as 'FOO BAR'. – Ankita Goyal Aug 23 '17 at 11:03
  • @AnkitaGoyal My understanding is that your example case _should_ fail, because `FOO BAR` does not contain every word in `FOO TAN BAR`. – Tim Biegeleisen Aug 23 '17 at 11:07
  • @TimBiegeleisen: your logic is also correct.I think strList is not required here.Instead you can check on string like if (!str.contains(sub)).No need to split the string. – Vaibs Aug 23 '17 at 11:30
  • But careful: If I did that, then the parent string `FOOSTUFF BAR` would appear to contain the word `FOO`, when in fact it really doesn't. – Tim Biegeleisen Aug 23 '17 at 11:32
  • Right. Missed it! – Vaibs Aug 23 '17 at 11:39