5
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?

LuftWoofe
  • 95
  • 1
  • 1
  • 6
  • 1
    http://stackoverflow.com/questions/5884353/insert-a-character-in-a-string-at-a-certain-position – Reimeus Sep 18 '16 at 15:18

8 Answers8

25

You can make something like below :

Convert your char array to string

   String b = new String("Tutorial");

then create 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
Ahmed Gamal
  • 1,666
  • 1
  • 17
  • 25
3

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();
}
Shahid
  • 2,288
  • 1
  • 14
  • 24
1

You could do something like this:

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

Simplest approach is to use 2 loops:

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);
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

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

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"));
  }
Gianmarco F.
  • 780
  • 2
  • 12
  • 36
0

One single loop, O(n) complexity

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);
}
blue
  • 539
  • 3
  • 7
0

you can do like this as row shifting without string manipulation 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);
    }
Md Ayub Ali Sarker
  • 10,795
  • 4
  • 24
  • 19
0
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);
    }
Lenin
  • 1
  • 1
    it would be good to explain your solution and how it addresses the original question. – Phil Jul 11 '17 at 01:21