Append a single character to a string or char array in java?

511,501

Solution 1

1. String otherString = "helen" + character;

2. otherString +=  character;

Solution 2

You'll want to use the static method Character.toString(char c) to convert the character into a string first. Then you can use the normal string concatenation functions.

Solution 3

new StringBuilder().append(str.charAt(0))
                   .append(str.charAt(10))
                   .append(str.charAt(20))
                   .append(str.charAt(30))
                   .toString();

This way you can get the new string with whatever characters you want.

Solution 4

First of all you use here two strings: "" marks a string it may be ""-empty "s"- string of lenght 1 or "aaa" string of lenght 3, while '' marks chars . In order to be able to do String str = "a" + "aaa" + 'a' you must use method Character.toString(char c) as @Thomas Keene said so an example would be String str = "a" + "aaa" + Character.toString('a')

Solution 5

just add them like this :

        String character = "a";
        String otherString = "helen";
        otherString=otherString+character;
        System.out.println(otherString);
Share:
511,501
CodeLover
Author by

CodeLover

Updated on July 08, 2022

Comments

  • CodeLover
    CodeLover almost 2 years

    Is it possible to append a single character to the end of array or string in java. Example:

    private static void /*methodName*/ () {            
        String character = "a"
        String otherString = "helen";
        //this is where i need help, i would like to make the otherString become 
        // helena, is there a way to do this?               
    }