Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?                } 
like image 444
CodeLover Avatar asked Jan 21 '13 18:01

CodeLover


People also ask

Can you append a character to a string?

Use the strncat() function to append the character ch at the end of str. strncat() is a predefined function used for string handling. string. h is the header file required for string functions.

Can a character be added to a string in Java?

One can also use String's substring method to add character to String at given position.

How do you add one to a char in Java?

In Java, char and int are compatible types so just add them with + operator. char c = 'c'; int x = 10; c + x results in an integer, so you need an explicit casting to assign it to your character varaible back.


2 Answers

1. String otherString = "helen" + character;  2. otherString +=  character; 
like image 156
Android Killer Avatar answered Oct 08 '22 19:10

Android Killer


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.

like image 29
Thomas Keene Avatar answered Oct 08 '22 19:10

Thomas Keene