Java 将字符添加到位置 x 的字符串中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39559218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:14:51  来源:igfitidea点击:

Add a char into a string at position x

javastringcharconcatenation

提问by LuftWoofe

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);
}

I'm trying to create a method where it adds an letter to a string, then returns it. So far I've been able to add a character at the front of the string, but I'm not quite sure how to do that in the middle/end. Inside the if-condition I'm pushing every letter a slot behind, so there's room for the new letter in the front. However I don't know what to do if I'm gonna add something in the middle, any tips?

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

采纳答案by Ahmed Gamal

You can make something like below :

您可以制作如下内容:

Convert your char array to string

将您的字符数组转换为字符串

   String b = new String("Tutorial");

then create StringBuilder

然后创建 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

回答by Eng.Fouad

Simplest approach is to use 2 loops:

最简单的方法是使用 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);

回答by Gianmarco F.

What about using the String.substring(int startindex, int end) method?

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

It should be something like this

它应该是这样的

    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"));
  }

回答by Shahid

You could go this way too:

你也可以这样:

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

回答by blue

One single loop, O(n) complexity

一个循环,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);
}

回答by Md Ayub Ali Sarker

you can do like this as row shifting without string manipulation api

你可以像这样在没有字符串操作 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);
    }

回答by Lenin

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);
    }

回答by Roberto Ierman

You could do something like this:

你可以这样做:

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