Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a char into a string at position x

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?

like image 358
LuftWoofe Avatar asked Sep 18 '16 15:09

LuftWoofe


People also ask

How do you add a character to a string position?

One can use the StringBuffer class method namely the insert() method to add character to String at the given position. This method inserts the string representation of given data type at given position in StringBuffer.

Can a char be added to a string?

Method 1: Add char to String Using Concatenation Operator “+” The easiest and most commonly used method to add char to a String is the Concatenation operator “+”. It concatenates the character to a String.

How do you put a character in a specific position in a string Python?

The next method of inserting the characters into a string is using a combination of a list(), insert(), and join() built-in functions of Python. By using this combination, we can convert the string into a list with the list() function and add the string at the targeted position with the insert() function.

How do I add a character to a string in C++?

C++ '+' operator for String Concatenation C++ '+' operator can be used to concatenate two strings easily. The '+' operator adds the two input strings and returns a new string that contains the concatenated string.


3 Answers

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
like image 179
Ahmed Gamal Avatar answered Sep 30 '22 23:09

Ahmed Gamal


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();
}
like image 25
Shahid Avatar answered Sep 30 '22 23:09

Shahid


You could do something like this:

string = string.substring(0,x) + "c" + string.substring(x, string.length());
like image 33
Roberto Ierman Avatar answered Sep 30 '22 23:09

Roberto Ierman