【问题标题】:Add a char into a string at position x在位置 x 处将 char 添加到字符串中
【发布时间】:2017-01-26 07:40:20
【问题描述】:
public String addLetter(char letter, int position, char[] word){
    char[]newWord = new char[word.length+1];

    if(position == 0){
        for(int i = position+1; i<word.length+1; i++){
            newWord[i] = word[i-1];
        }
        newWord[position] = letter;
    }else{

    }
    return new String(newWord);
}

我正在尝试创建一个方法,它将一个字母添加到一个字符串,然后返回它。到目前为止,我已经能够在字符串的前面添加一个字符,但我不太确定如何在中间/末尾添加一个字符。在 if 条件中,我将每个字母都推到后面一个槽,所以前面有新字母的空间。但是,如果我要在中间添加一些东西,我不知道该怎么办,有什么提示吗?

【问题讨论】:

标签: java string char concatenation


【解决方案1】:

你可以像下面这样:

将你的char数组转换为字符串

   String b = new String("Tutorial");

然后创建 StringBuilder

   StringBuilder str = new StringBuilder(b);
   System.out.println("string = " + str);

   // insert character at offset 8
   str.insert(8, 's');

   // print StringBuilder after insertion
   System.out.print("After insertion = ");
   System.out.println(str.toString());// this will print Tutorials

【讨论】:

    【解决方案2】:

    最简单的方法是使用 2 个循环:

    char[] newWord = new char[word.length + 1];
    
    for(int i = 0; i < position; i++) newWord[i] = word[i];
    newWord[position] = letter;
    for(int i = position + 1; i < newWord.length; i++) newWord[i] = word[i - 1];
    
    return new String(newWord);
    

    【讨论】:

      【解决方案3】:

      使用 String.substring(int startindex, int end) 方法怎么样?

      应该是这样的

          public static String addLetter(char letter, int position, String word){
          String toSupport = "";
      
          if(position == 0){
             toSupport += letter +word;
          } else {
              String temp = word.substring(0, position+1);
              toSupport += temp + Character.toString(letter) + word.substring(position+1, word.length());
          }
          return toSupport;
      }
      
        public static void main(String[] args) {
           System.out.println(addLetter('a', 1, "hello"));
        }
      

      【讨论】:

        【解决方案4】:

        你也可以这样:

        public String addLetter(char letter, int position, char[] word) {
            return new StringBuilder(new String(word)).insert(position, letter).toString();
        }
        

        【讨论】:

          【解决方案5】:

          单循环,O(n) 复杂度

          public String addLetter(char letter, int position, char[] word){
              char[]newWord = new char[word.length+1];
          
              int offset = 0;
              for(int i=0; i<newWord.length; i++) {
                  if(position == i) {
                      newWord[i] = letter;
                      offset = 1;
                  } else {
                      newWord[i] = word[i-offset];
                  }
              }
          
              return new String(newWord);
          }
          

          【讨论】:

            【解决方案6】:

            你可以像这样在不使用字符串操作 api 的情况下进行行移位

            public String addLetter(char letter, int position, char[] word) {
                    char[] newWord = new char[word.length + 1];
                    int i;
                    for (i = word.length; i >= position; i--) {
                        newWord[i] = word[i-1];     
                    }
            
                    newWord[i] = letter;
            
                    while(i>0){
                        newWord[--i] = word[i];
                    }
            
                    return new String(newWord);
                }
            

            【讨论】:

              【解决方案7】:
              private static String insertChar(String word, char letter, int position) {
                      char[] chars = word.toCharArray();
                      char[] newchars = new char[word.length() + 1];
              
                      for (int i = 0; i < word.length(); i++) {
                          if (i < position)
                              newchars[i] = chars[i];
                          else
                              newchars[i + 1] = chars[i];
                      }
                      newchars[position] = letter;
                      return new String(newchars);
                  }
              

              【讨论】:

              • 最好解释一下您的解决方案以及它如何解决原始问题。
              【解决方案8】:

              你可以这样做:

              string = string.substring(0,x) + "c" + string.substring(x, string.length());
              

              【讨论】:

              • .length() 不是函数。只需使用 .length 或完全省略它 string.substring(x)
              猜你喜欢
              • 2017-09-01
              • 2020-08-17
              • 2011-04-03
              • 2023-03-11
              • 1970-01-01
              • 2022-01-10
              • 2021-03-18
              • 1970-01-01
              • 2020-02-26
              相关资源
              最近更新 更多