【问题标题】:Replace specific characters in a string without using replace method in Java在Java中不使用replace方法替换字符串中的特定字符
【发布时间】:2023-03-14 10:03:01
【问题描述】:

我不允许使用 replace() 方法,我基本上必须让我的程序的一部分来执行此操作:

-要求用户输入要操作的句子。 - 然后要求用户输入要替换的字符(即“hello”中的“e”)。 -然后询问他们希望将其替换为哪个字符。

完成所有这些后,它会将被操纵的字符串输出到屏幕上。

示例:

  • 控制台:请输入字符串:
  • 用户:你好
  • 控制台:请输入要替换的字符:
  • 用户:e
  • 控制台:请输入一个字符来替换它:
  • 用户:l
  • 控制台:新字符串是:Hi thlrl

我基本上可以做到这一点,使用这段代码:

    int count = 1;
    char replaceAll = 'a';
    char newChar = 'b';
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter the string to be manipulated");
    String sentence = keyboard.nextLine();

        int count = 0;
        System.out.println("Enter the character to replace");
        replaceAll = keyboard.nextLine().charAt(0);
        System.out.println("Enter the new character");
        newChar = keyboard.nextLine().charAt(0);
        System.out.print("The new sentence is: ");

        for(int c = 0; c < sentence.length(); c++){
            if(sentence.charAt(c) == replaceAll)
                System.out.print(newChar);
            else
                System.out.print(sentence.charAt(c));

主要问题:唯一的问题是这不会将操作的字符串保存为新字符串。它只会以正确的顺序打印字母以进行输出(一次打印一个字符,而不是一个字符串)。我需要将已操作的字符串(即 Hi thlrl)保存为新字符串,以便之后可以对该新字符串进行更多操作。

【问题讨论】:

    标签: java replace char string-parsing


    【解决方案1】:

    您可以根据您的sentence 大小声明char[],然后在循环中填充它。

      char[] ch = new char[sentence.length()];
    
      for(int c = 0; c < sentence.length(); c++)
       {
            if(sentence.charAt(c) == replaceAll)
            {
                System.out.print(newChar);
                ch[c]=newChar;
            }
            else
            {
                System.out.print(sentence.charAt(c));
                ch[c]=sentence.charAt(c);
             }
         }         
    

    其他可能的解决方案是,您可以使用toCharArray() 创建char[]

      char[] ch = sentence.toCharArray();
    
      for(int c = 0; c < sentence.length(); c++)
       {
            if(sentence.charAt(c) == replaceAll)
            {
                ch[c]=newChar;
            }
       }
    

    然后,使用char[] 创建String

      String mystring = new String(ch);
    

    【讨论】:

      【解决方案2】:

      如果是 Stringbuffer/StringBuilder 那么更简单的是使用 setCharAt

         StringBuilder sentenceStr = new StringBuilder(sentence); 
         sentenceStr.setCharAt(c, newChar);
      

      【讨论】:

        【解决方案3】:

        您可以使用split 将字符串切成小块,在每次出现要替换的字符时,然后使用join 将其重新组合在一起。例如,

        String.join("l", "Hello there".split("e", -1));
        

        计算为"Hllllo thlrl"

        请注意,-1 很重要 - 如果它是您尝试拆分的元素,它会阻止最终元素被丢弃。

        【讨论】:

          【解决方案4】:

          你可以初始化一个空字符串,当你遍历你的句子字符串时,你可以像这样构建新的空字符串:

          String result = "";
          for(int c = 0; c < sentence.length(); c++) {
              if(sentence.charAt(c) == replaceAll) {
                  result += newChar;
              } else {
                 result += sentence.charAt(c);
              }
           }
          System.out.println(result);
          

          【讨论】:

            【解决方案5】:

            /* char 'o' 替换为 char 'd' 而没有替换功能 */

                String oldString = "welcome mojo";
            
                String[] StringArray = oldString.split("");
            
                String newString = "";
            
                int count = StringArray.length;
            
                for (int i = 0; i < count; i++) {
            
                    if (StringArray[i].equalsIgnoreCase("o")) {
            
                        StringArray[i] = "d";
                    }
            
                    newString = newString + StringArray[i];
            
                }
                System.out.println(newString);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-10-16
              • 2016-01-27
              • 1970-01-01
              • 1970-01-01
              • 2017-03-06
              • 1970-01-01
              • 1970-01-01
              • 2012-08-09
              相关资源
              最近更新 更多