0

I need to check if a String with several words is at the beginning of another one. For example, I have a list of Strings containing things like: "in the morning", "in the evening", etc, and I want to check if my string st = "in the evening at 5" starts with any of the Strings in my list. I need a boolean answer to this and ideally a StringTokenizer with the remaining part "at 5".

My problem is that right now I have the string st splitted by a StringTokenizer (I needed it for a different reason), so I can't just used the methods substring or indexOf to check if it's contained.

Is there any simple way to do it? Or is there a way of converting the remaining items of the Tokenizer to a String again?

Thank you

randombee
  • 699
  • 1
  • 5
  • 26
  • What_have_you_tried? – Andremoniy Dec 01 '15 at 11:22
  • I haven't tried any solution yet because the only ideas I had involved iterating through the tokens and checking one by one, and I wasn't sure how to "go back" to check in case I had a false match. – randombee Dec 01 '15 at 11:24
  • First of all, please don't use StringTokenizer. Like the [JavaDoc](http://docs.oracle.com/javase/8/docs/api/java/util/StringTokenizer.html) says, it is a legacy which shouldn't be used anymore. And we also need to know how your `st` now looks like. Please share your code. – Tom Dec 01 '15 at 11:24
  • You should demonstrate minumul efforts for soilving this issue. Say provide an example how you have tried to solve it, but it is not efficient and you want to improve its perfomance. – Andremoniy Dec 01 '15 at 11:24
  • Read some text matching / fuzzy search / string search techniques. Google is your best friend – aksappy Dec 01 '15 at 11:29
  • @Tom I can't avoid using StringTokenizer because it's not my own code and I can't change this. It's using Locale settings. – randombee Dec 01 '15 at 11:32
  • @aksappy I couldn't find anything related to reconverting the remaining of a StringTokenizer back to it's original String and I wanted to avoid looping as much as possible, which is the answer Google is giving me pretty much... – randombee Dec 01 '15 at 11:33

2 Answers2

1

Maybe something like this?

private static String[] keys = {"in the morning", "in the evening"};

class Hit {
    String key;
    String remainder;
    Hit(String key, String remainder) {
        this.key = key;
        this.remainder = remainder;
    }
}

public Optional<Hit> findHit(String input) {
    for(String key : keys) {
        if (input.startsWith(key)) {
            return Optional.of(new Hit(key, input.substring(key.length())));
        }
    }
    return Optional.empty();
}

EDIT: relevant question Get Position in Original String from `StringTokenizer`

Community
  • 1
  • 1
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
0

Try this:

String sStr = "in the morning";
String str = "in the morning at 5";
List<String> searchStringList = new ArrayList<String>();
searchStringList.add(sStr);

for(String searchString : searchStringList) {
    if(str.startsWith(searchString)) {
        return true;
    }
}
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
Deepak Kumar
  • 843
  • 1
  • 7
  • 19
  • Thank you. The problem is that we need to check things like the day before we get to the point where we check this part and we want the remainder of the string to perform some validations. Also, we are using Locale settings, that's why they use the StringTokenizer. – randombee Dec 01 '15 at 11:42
  • 1
    Anyway, I'm accepting this answer because it's better to add the Locale settings just in the remainder of the String anyway. I will create the StringTokenizer after performing this operation. So just going to use plain Strings and the startsWith method as suggested. – randombee Dec 01 '15 at 11:55