【问题标题】:How do I replace all the letters in a string with another character using Java?如何使用 Java 将字符串中的所有字母替换为另一个字符?
【发布时间】:2023-04-09 13:53:01
【问题描述】:

我希望能够用下划线字符替换字符串中的所有字母。

例如,假设我的字符串由字母字符组成:“Apple”。我如何将其转换为五个下划线,因为苹果中有五个字符(字母)?

【问题讨论】:

  • 所有字符还是所有字母?
  • 你应该尝试一下。告诉我们。

标签: java


【解决方案1】:

您可以使用String.replaceAll() 方法。

替换所有字母:

String originalString = "abcde1234";
String underscoreString = originalString.replaceAll("[a-zA-Z]","_");

如果你的意思是所有字符:

String originalString = "abcde1234";
String underscoreString = originalString .replaceAll(".", "_");

【讨论】:

  • originalString.replaceAll("[a-zA-Z]+","_");改成originalString.replaceAll("[a-zA-Z]","_");,否则只会输出1个下划线
【解决方案2】:

为什么不忽略“替换”的想法,简单地创建一个具有相同数量下划线的新字符串...

String input = "Apple";

String output = new String(new char[input .length()]).replace("\0", "_");
//or
String output2 = StringUtils.repeat("_", input .length());

大部分来自here

正如许多其他人所说,如果您不想包含空格,replaceAll 可能是要走的路。为此,您不需要正则表达式的全部功能,但除非字符串绝对巨大,否则肯定不会受到伤害。

//replaces all non-whitespace with "_"
String output3 = input.replaceAll("\S", "_");

【讨论】:

    【解决方案3】:
            String content = "apple";
            String replaceContent = "";
            for (int i = 0; i < content.length(); i++)
            {
                replaceContent = replaceContent + content.replaceAll("^\\p{L}+(?: \\p{L}+)*$", "_");
            }
    
            System.out.println(replaceContent);
    

    使用Regular Expression

    关于\p{L}:参考Unicode Regular Expressions

    【讨论】:

    • 嗯?你的正则表达式是心理的
    【解决方案4】:

    试试这个。

    String str = "Apple";
    str = str.replaceAll(".", "_");
    System.out.println(str);
    

    【讨论】:

    • 这不会有五个下划线吧?据我所知只有一个
    • @poohdedoo 不,你会得到五个下划线
    【解决方案5】:

    试试这个,

            String sample = "Apple";
            StringBuilder stringBuilder = new StringBuilder();
            for(char value : sample.toCharArray())
            {
                stringBuilder.append("_");
            }
            System.out.println(stringBuilder.toString());
    

    【讨论】:

      【解决方案6】:
      stringToModify.replaceAll("[a-zA-Z]","_");
      

      【讨论】:

        【解决方案7】:

        你可以的

        int length = "Apple".length();
        String underscores = new String(new char[length]).replace("\0", "_");
        

        【讨论】:

          【解决方案8】:

          str.replaceAll("[a-zA-Z]","_");


              String str="stackoverflow";
              StringBuilder builder=new StringBuilder();
              for(int i=0;i<str.length();i++){
                  builder.append('_');
              }
              System.out.println(builder.toString());
          

          【讨论】:

            【解决方案9】:

            当然,我会折腾一个替代方案:

            String input = "Apple";
            char[] newCharacters = new char[input.length()];
            Arrays.fill(newCharacters, '_');
            String underscores = new String(newCharacters);
            

            【讨论】:

              【解决方案10】:

              或者这是一个不错的递归方法:

              public static void main(String[] args) {
                  System.out.println(underscores("Apple"));
              }
              
              public static String underscores(String input) {
                  if (input.isEmpty()) return "";
                  else return "_" + underscores(input.substring(1));
              }
              

              【讨论】:

                【解决方案11】:

                这个呢

                  public static void main(String args[]) {
                    String word="apple";
                
                        for(int i=0;i<word.length();i++) {
                            word = word.replace(word.charAt(i),'_');
                        }
                        System.out.println(word);
                }
                

                【讨论】:

                • 为什么会出现这种情况? if (word.length()== 5)
                • 我想我看错了他的要求.. 我以为他只想转换如果他只得到五个字符的单词
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2021-12-10
                • 2020-08-27
                • 2012-07-16
                • 1970-01-01
                • 2014-01-31
                • 1970-01-01
                • 2014-05-18
                相关资源
                最近更新 更多