user7691120
user7691120

Reputation:

Java string get the character after space

I was having some problem when trying to compare strings in Java.

Example of list of words to be compared:

angry berry you young your apple orange yeast

When user entered 'y' character, the results I am getting as text suggestion list:

you young your yeast

At this point, it is correct. Then I entered 'you '. There is a space afterwards. At this point the text suggestion list should not be coming out. But it did. Then I proceed to enter 'b' so 'berry' should be showing as suggestion list which it did.

I not sure why whenever I entered a space, the suggestion list is still showing. There is something wrong with the sequence. Here is my code:

String input = editTextInput.getText().toString();

// getting text after space
String[] segments = input.split(" ");
String lastInputSegment = segments[segments.length - 1];

// if user input not null, proceed to find matching keywords
if(!lastInputSegment.equals("")) {
      // find matching keywords
      // display list of matching keywords
}

Any ideas? Thanks!

Upvotes: 1

Views: 9946

Answers (4)

Lino
Lino

Reputation: 19926

@Tim Biegelseisens answer works very well. But you can improve it by using Patterns, because String.matches() always creates a pattern and a matcher itself.

From the jdk1.8.0_131 sources of String.matches():

public boolean matches(String regex) {
    return Pattern.matches(regex, this);
}

Source of Pattern.matches():

public static boolean matches(String regex, CharSequence input) {
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(input);
    return m.matches();
}

So by using the following snippet you get rid of creating a new Pattern for every input string you're iterating over:

String input = "angry berry you young your apple orange yeast";
Pattern search = Pattern.compile("you .*");   // our search pattern

Pattern.compile(" ").splitAsStream(input)     // creates a Stream<String>
    .filter(s -> search.matcher(s).matches()) // checks if string matches
    .forEach(System.out::println);

Upvotes: 0

daniu
daniu

Reputation: 14999

You should probably use Scanner instead of String.split().

try (Scanner sc = new Scanner(editInputText())) {
    while (sc.hasNext()) {
        String item = sc.next(); // add filter here
    }
}

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521289

If you want to use pattern matching as part of your search logic, then regex would seem to be a good option. We can easily handle this using streams and String.matches:

String input = editTextInput.getText().toString();
String[] segments = input.split(" ");
String search = "you ";

Stream<String> stream = Arrays.stream(segments);
stream.filter(s -> s.matches(search + ".*"))
    .forEach(x -> System.out.println(x));

Demo

Edit:

If the behavior you want to is to ignore all trailing whitespace on the search term, however many words it might be, then you can simply call String.trim() on the search term:

String search = "you ";
search = search.trim();
// then use the same logic above

Upvotes: 3

Sven Hakvoort
Sven Hakvoort

Reputation: 3621

You use input.split(" ") which splits on a whitespace, causing the trailing whitespace to be removed and thus it still finds the String.

Example:

Input: "you something"
Becomes: ["you", "something"]

So following that:

Input: "you "
Becomes: ["you"]

If you do want to keep the empty string, you can do the following:

String[] segments = input.split(" ", -1);

A negative value for split indicates that it should not ignore the empty strings. Please note that this will cause all the whitespaces between the words to be preserved.

Upvotes: 8

Related Questions