【问题标题】:How to check if a word ends and starts with a common symbol and replace it as many times it appears with 1如何检查一个单词是否以通用符号结尾和开头并将其替换为 1
【发布时间】:2019-03-03 01:11:22
【问题描述】:

我面临一个小挑战,这就是我一直在尝试做的事情。

假设我有这两个变量

String word1 ="hello! hello!! %can you hear me%? Yes I can.";

And then this one

String word2 ="*Java is awesome* Do you % agree % with us?";

我希望能够检查一个变量是否包含一个以我正在使用和替换的特定符号(如 % 和 *)开头和结尾的单词;带有“1”(一个)。这是我尝试过的。

StringTokenizer st = new StringTokenizer(word1);


while(st.hasMoreTokens()){
  String block = st.nextToken();
 if( (block.startsWith("%") && block.endsWith("%") ||(block.startsWith("*") && block.endsWith("*")){
   word1.replace (block,"1");
}
}

//output
'hello!hello!!%canyouhearme%?YesIcan."

//expected
"hello! hello!! 1? Yes I can.";

它刚刚修剪它。我想这是因为使用的分隔符是空格,并且最后一个 % 以 %?它将它作为一个单独的块读取。

When I tried the same for word2  

I got "1Doyou%agree%withus?"

//expected 
"1 Do you 1 with us?"

假设我还有另一个词,比如

String word3 ="%*hello*% friends";
I want to be able to produce 

 //output
 "1friends"

//expected
"11 friends"

Since it has 4-symbols 

任何帮助将不胜感激,只是提高我的 Java 技能。谢谢。

【问题讨论】:

  • 我认为您不需要令牌。您可以使用charAt() 来检查您的符号并使用replaceAll() 将其替换为 1
  • %*hello*%如何翻译成数字11,这4个符号是从哪里来的?
  • @surendrapanday 和哪个计数系统是 4? hello 有 5 个字符...不确定 * 消失在哪里,怎么变成 11?
  • @surendrapanday 但这应该导致“1”,而不是“11”,因为他从未说过不应该替换“%1%”之类的东西,而是在现有的后面附加另一个“1” .
  • 好吧,他指的是特殊字符的特殊符号。 %**% 是四个符号,而不是应该说特殊字符,这就是他的错误。

标签: java


【解决方案1】:

您可以在String.matches() 方法中使用Regular Expression (RegEx) 来确定字符串是否包含特定条件,例如:

if (word1.matches(".*\\*.*\\*.*|.*\\%.*\\%.*")) {
    // Replace desired test with the value of 1 here...
}

如果您想了解此正则表达式的完整说明,请转到 rexex101.com 并输入以下表达式:.*\*.*\*.*|.*\%.*\%.*

上面的 if 语句条件利用 String.matches() 方法来验证字符串是否在星号 (*) 之间包含文本(或不包含文本)或百分比 (%) 字符之间。如果是这样,我们只需使用String.replaceAll() 方法将那些字符串部分(介于*...*%...% 之间)替换为值1,如下所示:

String word1 = "hello! hello!! %can you hear me%? Yes I can.";
if (word1.matches(".*\\*.*\\*.*|.*\\%.*\\%.*")) {
    String newWord1 = word1.replaceAll("\\*.*\\*|%.*%", "1");
    System.out.println(newWord1);
}

控制台窗口将显示:

hello! hello!! 1? Yes I can.

如果您要使用上述代码播放此字符串:"*Java is awesome* Do you % agree % with us?",您的控制台窗口将显示:

1 Do you 1 with us?

请记住,如果您提供的字符串是"** Do you %% with us?",这将为控制台提供相同的输出。如果您真的不想要这个,那么您将需要在 ma​​tches() 方法中将 RegEx 修改为如下所示:

".*\\*.+\\*.*|.*\\%.+\\%.*"

您需要将 replaceAll() 方法中的 RegEx 修改为:

"\\*.+\\*|%.+%"

通过此更改,现在星号和/或百分比字符之间必须有文本,才能成功验证并进行更改。

【讨论】:

  • 一直在研究正则表达式以及您从 d 链接发布的所有内容。现在,能够实现我想要的。谢谢你。
【解决方案2】:

问题不清楚(不确定%*hello*% 是如何转换为 11 的,并且不明白您所说的 Since it has 4-symbols 是什么意思),但是正则表达式不起作用吗?

你不能简单地做:

String replaced = word1.replaceAll("\\*[^\\*]+\\*", "1")
                       .replaceAll("\\%[^\\%]+\\%", "1");

【讨论】:

    【解决方案3】:

    我会说你假设特殊字符将被替换两次是错误的。当您尝试替换出现的字符串时,替换功能仅适用于大小写,这似乎不适用于特殊字符。只有replaceAll,似乎在这种情况下有效。在您的代码中,您试图替换特殊字符以及其中的其他字符串,因此只有 replaceAll 函数会这样做。

    换句话说,当 replaceAll 函数被执行时,它会检查特殊字符的出现,并替换它一次。您不需要使用 StringTokenizer,它是 Scanner 库的一部分,只有在您接受用户输入时才需要它。所以,无论你做什么,你只会看到 1 friends 而不是 11 friends ,而且你也不需要 if 语句。正则表达式的信用转到上面的jbx。现在,您可以像这样缩短您的代码,但请记住,1 被打印替换 whatever 内部特殊字符被单个数字 1 替换。

    您将需要if-statement 来搜索、replaceAll 或 replace 函数已经在您指定搜索的String 中搜索,因此 if 语句是多余的,它只会让代码变得冗长。

    package object_list_stackoverflow;
    import java.util.StringTokenizer;
    public class Object_list_stackoverflow {
        public static void main(String[] args) {
            String word1 = "hello! hello!! %can you hear me%? Yes I can.";
            String word2 ="*Java is awesome* Do you % agree % with us?";
            String word3 ="%*hello*% friends";
            String regex = "\\*[^\\*]+\\*";
            String regex1= "\\%[^\\%]+\\%";       
      System.out.println(word3.replaceAll(regex, "1").replaceAll(regex1, "1")); 
           }   
        }
    

    也可以阅读类似的问题:Find Text between special characters and replace string
    您还可以通过查看 dhuma1981 的答案来摆脱字母数字字符:How to replace special characters in a string?

    替换字符串中字母数字的语法:

    replaceAll("[^a-zA-Z0-9]", "");
    

    【讨论】:

      猜你喜欢
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      相关资源
      最近更新 更多