简体   繁体   English

Java:检查字符串是否包含多个字符

[英]Java: check if string contains more than one character

I would like to check my inputted string on a comma, which is pretty easy ( just used this: How can I check if a single character appears in a string? ). 我想用逗号检查我输入的字符串,这很容易(仅使用此方法: 如何检查字符串中是否出现单个字符? )。 Now I want to check if a character is present more than once. 现在,我想检查一个字符是否存在多次。 (in this case a comma (/u002C)) I have created this code: (在这种情况下为逗号(/ u002C))我创建了以下代码:

public static void addTextLimiterDouble(final TextField tf, final int maxLength, final Button submitButton) {
    tf.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(final ObservableValue<? extends String> ov, final String oldValue, final String newValue) {

            if (oldValue.contains("\u002C") && newValue.endsWith("\u002C")) {
                tf.setText(oldValue);
            }
    }
});

This gives a StackOverflow error, because it seems it can not get out of the loop. 这给出了一个StackOverflow错误,因为它似乎无法脱离循环。 Can anyone explain why it can't? 谁能解释为什么不能呢?

Note: I know there are maybe more ways to check for more than one character, but I am just curious why this gives me an error. 注意:我知道可能有更多方法来检查多个字符,但是我很好奇为什么这会给我一个错误。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Imagine the situation (actually, that's most likely the situation where you are in) where you are editing a string that ends in a comma. 想象一下您正在编辑以逗号结尾的字符串的情况(实际上,很可能是您所处的情况)。 When you change something else in that string, then your if statement ( if (oldValue.contains("\,") && newValue.endsWith("\,")) ) returns true and you try to revert to the old value by calling tf.setText() 当您更改该字符串中的其他内容时,您的if语句( if (oldValue.contains("\,") && newValue.endsWith("\,")) )返回true ,您尝试通过调用恢复为旧值tf.setText()

However- that changes the text! 但是,这会更改文本! And your ChangeListener is invoked again. 并且您的ChangeListener再次被调用。 The new value ends in a comma (you were reverting back to a value that ends in a comma) and the old value also contains a comma, so again, your listener calls tf.setText() . 新值以逗号结尾(您将还原为以逗号结尾的值),而旧值也包含逗号,因此,您的侦听器再次调用tf.setText()

After that, it invokes your ChangeListener because the text was changed. 之后,由于文本已更改,因此它将调用您的ChangeListener And again... ad nauseum 并再次... 广告nauseum

String.endsWith checks if the passed String is present at the end of the String calling the method, but that is not what you are willing to do. String.endsWith检查在调用该方法的String的末尾是否存在传递的String ,但这不是您愿意做的。

This is what I would do : 这就是我要做的:

int index = myString.indexOf(ch);
boolean moreThanOnce = index != -1 && index != myString.lastIndexOf(ch);

However your test seems weird to me since it tests something on both oldValue and newValue . 但是,您的测试对我来说似乎很奇怪,因为它同时测试了oldValuenewValue If I understood well what you want to do you should only consider one of those two. 如果我对您要做什么很了解,则应该只考虑这两个之一。

You add a Listener to your TextField. 您将侦听器添加到您的TextField。 Then you override the changed() method. 然后,您覆盖了changed()方法。 In this method you call setText() !! 在这种方法中,您调用setText() This will invoke the Listeners' changed() again .... so you end up in an infinite loop ... 这将再次调用侦听器的changed() ....因此您最终陷入无限循环...

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

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