67

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?               
}
7
  • 1
    i have tried the append method but i am very confused about how to use it... Commented Jan 21, 2013 at 18:12
  • 5
    So how have you tried to use the append method? And have you tried simple string concatenation via +? Note that strings and arrays are entirely different things... Commented Jan 21, 2013 at 18:13
  • 2
    Sinnce they are string you can use the builtin string concatenation mechanism of the language by doing otherString + character here. Commented Jan 21, 2013 at 18:13
  • 1
    You are thinking of the StringBuilder class with the append method. Commented Jan 21, 2013 at 18:13
  • 10
    Show more research effort. Don't post a question here every time you forget something/want to know some basic operation/method. Commented Jan 21, 2013 at 18:18

7 Answers 7

124
1. String otherString = "helen" + character;

2. otherString +=  character;
Sign up to request clarification or add additional context in comments.

Comments

11

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.

Comments

9
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.

Comments

3

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')

Comments

3

And for those who are looking for when you have to concatenate a char to a String rather than a String to another String as given below.

char ch = 'a';
String otherstring = "helen";
// do this
otherstring = otherstring + "" + ch;
System.out.println(otherstring);
// output : helena

3 Comments

Thanks for your answer, but I'd advise reviewing how this works in Java. The accepted answer works regardless of whether the variable named character is a 'char' or a 'String' and the empty String in your answer: [otherstring + "" + ch] is unnecessary. Also, I do not think that this question requires another new answer as the existing answers provide sufficient coverage.
I answered this question because if you look closely, you will find out that the question title is a little vague and is not related to the question which user intended to ask and I tried to answer exactly that. cheers!!
The need of adding an empty. "" in the line otherstring = otherstring + "" + ch; is totally useless.
2

just add them like this :

        String character = "a";
        String otherString = "helen";
        otherString=otherString+character;
        System.out.println(otherString);

Comments

0
public class lab {
public static void main(String args[]){
   Scanner input = new Scanner(System.in);
   System.out.println("Enter a string:");
   String s1;
   s1 = input.nextLine();
   int k = s1.length();
   char s2;
   s2=s1.charAt(k-1);
   s1=s2+s1+s2;
   System.out.println("The new string is\n" +s1);
   }
  }

Here's the output you'll get.

* Enter a string CAT The new string is TCATT *

It prints the the last character of the string to the first and last place. You can do it with any character of the String.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.