【问题标题】:How to remove all instances of a character in textField using JButton?如何使用 JButton 删除 textField 中字符的所有实例?
【发布时间】:2013-11-17 02:51:05
【问题描述】:

我无法使用我的 JButton 从我的 textField 中删除不需要的字符。 到目前为止,我已经尝试使用 for 循环进行按钮调用以遍历字符并将所有字母字母实例替换为 0。但是该按钮实际上并没有做任何事情。任何帮助将不胜感激,这是我的代码:

    if(event.getSource() == call)
    {
        int length = search.getText().length();

        for(int i = 0; i < length; i++)
        {
            char a;
            a = search.getText().charAt(i);
            if(!(a >= '0' && a <= '9') || !(a == '#') || !(a == '*')
            {
                  search.getText().replace(Character.toString(a), "");
            }
        }
     }

search 是我的 textField 的名称,我想删除不在 0-9 或 # 或 * 键之间的任何字符。

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java string jbutton textfield


【解决方案1】:

这一行search.getText().replace(Character.toString(a), "");

应该是这样的

    search.setText(search.getText().replace(Character.toString(a), ""));

您可以这样做,而不是为每次迭代更改和更新一个字符。

    String searchStr= search.getText();
    for(int i = 0; i < searchStr.length(); i++)
    {
        char a;
        a = searchStr.charAt(i);
        if(!(a >= '0' && a <= '9') || !(a == '#') || !(a == '*')
        {
             searchStr = searchStr.replace(Character.toString(a), "");
        }
    }
    search.setText(searchStr);

【讨论】:

  • 非常感谢!它正在工作,但并不完全按照我想要的方式,我只需要调整我的 if 逻辑。再次感谢!
  • @FriarBob 如果这回答了您的问题,请将其标记为正确答案。
【解决方案2】:

当使用replace 方法时,它会返回字符串。

/**
 * Replaces each substring of this string that matches the literal target
 * sequence with the specified literal replacement sequence. The
 * replacement proceeds from the beginning of the string to the end, for
 * example, replacing "aa" with "b" in the string "aaa" will result in
 * "ba" rather than "ab".
 *
 * @param  target The sequence of char values to be replaced
 * @param  replacement The replacement sequence of char values
 * @return  The resulting string
 * @throws NullPointerException if <code>target</code> or
 *         <code>replacement</code> is <code>null</code>.
 * @since 1.5
 */
public String replace(CharSequence target, CharSequence replacement) {
    return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
            this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}

所以当你使用replace的时候,你应该像下面这样使用它:

` searchStr = searchStr.replace(Character.toString(a), ""); `

而不是searchStr.replace(Character.toString(a), "");

这样可以替换值。

如果你之前使用的代码searchStr.replace(Character.toString(a), "");字符串值searchStr,不会改变。

此外,您还可以使用正则表达式来删除意外字符。 喜欢,

if(event.getSource() == call)
{
       //remove any character that is not between 0-9 or the # or * keys.
            String text = search.getText();
            text = text.replaceAll("[^0-9#*]", "");
            //Set new text to search
            search.setText(text);
}

【讨论】:

  • 也非常感谢!这比单独遍历每个字符更好地清除逻辑。我对正则表达式了解不多,你能解释一下“^[0-9*#]”在做什么吗?我的猜测是,它是说删除除“[]”分隔的所有内容?
  • [^0-9*#] 当插入符号作为方括号内的第一个字符出现时,它会否定模式。这可以匹配除从 0 到 9 或 * 或 # 的任何数字之外的任何字符。您可以从以下网址轻松获取正则表达式基础知识。 vogella.com/articles/JavaRegularExpressions/article.html
  • 好的,非常感谢,这是一个信息量很大的链接。
猜你喜欢
  • 2014-04-06
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2019-12-03
相关资源
最近更新 更多