简体   繁体   English

Java字符串在空格后获取字符

[英]Java string get the character after space

I was having some problem when trying to compare strings in Java. 尝试在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: 当用户输入“ y”字符时,我得到的结果作为文本建议列表:

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. 然后我继续输入“ b”,因此“ berry”应作为建议列表显示。

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! 谢谢!

You use input.split(" ") which splits on a whitespace, causing the trailing whitespace to be removed and thus it still finds the String. 您可以使用input.split(" ")在空格上分割,从而删除尾随空格,从而仍然找到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. 请注意,这将导致保留单词之间的所有空白。

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 : 我们可以使用stream和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.trim()

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

You should probably use Scanner instead of String.split() . 您可能应该使用Scanner而不是String.split()

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

@Tim Biegelseisen s answer works very well. @Tim Biegelseisen的答案非常有效。 But you can improve it by using Pattern s, because String.matches() always creates a pattern and a matcher itself. 但是您可以使用Pattern来改进它,因为String.matches()总是创建一个模式和一个匹配器本身。

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

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

Source of Pattern.matches() : 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: 因此,通过使用以下代码段,您Pattern为要迭代的每个输入字符串创建新的Pattern

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM