Can You Add Characters To A String Java
7 Answers vii
ane. String otherString = "helen" + character; 2. otherString += character;
answered Jan 21, 2013 at 18:13
Android KillerAndroid Killer
17.7k 12 gold badges 64 argent badges 87 statuary badges
You'll want to use the static method Grapheme.toString(char c) to convert the character into a cord first. Then you can use the normal string concatenation functions.
answered Sep 19, 2013 at 23:22
new StringBuilder().append(str.charAt(0)) .append(str.charAt(10)) .append(str.charAt(20)) .append(str.charAt(xxx)) .toString();
This way you can get the new string with whatever characters you desire.
tom redfern
29.3k 12 gold badges 92 silver badges 118 bronze badges
answered Oct 13, 2016 at 12:nineteen
Ankit JainAnkit Jain
two,156 ane gold badge 16 silver badges 25 bronze badges
First of all you lot use hither two strings: "" marks a string information technology may be ""
-empty "s"
- cord of lenght 1 or "aaa"
string of lenght 3, while '' marks chars . In guild to be able to do String str = "a" + "aaa" + 'a'
y'all must use method Character.toString(char c) as @Thomas Keene said so an example would be String str = "a" + "aaa" + Character.toString('a')
answered January 15, 2014 at 11:51
Bogdan One thousand.Bogdan M.
2,091 half-dozen gold badges 30 silver badges 52 statuary badges
just add them similar this :
String grapheme = "a"; String otherString = "helen"; otherString=otherString+character; Arrangement.out.println(otherString);
answered Jan 21, 2013 at eighteen:thirteen
Alya'a GamalAlya'a Gamal
five,554 xviii silvery badges 34 bronze badges
0
And for those who are looking for when you accept to concatenate a char to a String rather than a String to another String equally given below.
char ch = 'a'; String otherstring = "helen"; // do this otherstring = otherstring + "" + ch; System.out.println(otherstring); // output : helena
answered Jul 28, 2018 at 17:50
skmangalamskmangalam
319 two silver badges viii statuary badges
3
public class lab { public static void principal(String args[]){ Scanner input = new Scanner(Organisation.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; Organization.out.println("The new cord is\n" +s1); } }
Here's the output you'll get.
* Enter a cord CAT The new string is TCATT *
It prints the the final graphic symbol of the cord to the first and last place. You can exercise it with any character of the String.
answered Mar 24, 2015 at 5:18
yugantaryugantar
i,551 ten silver badges 17 statuary badges
Non the respond you're looking for? Browse other questions tagged java string append or ask your own question.
Can You Add Characters To A String Java,
Source: https://stackoverflow.com/questions/14444717/append-a-single-character-to-a-string-or-char-array-in-java
Posted by: galindowhistamed1951.blogspot.com
Thanks for your answer, only I'd advise reviewing how this works in Java. The accustomed reply 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. Likewise, I do not think that this question requires another new reply as the existing answers provide sufficient coverage.
Jul 28, 2018 at 18:43
I answered this question considering if yous look closely, you will find out that the question championship is a piddling vague and is not related to the question which user intended to ask and I tried to answer exactly that. cheers!!
Dec 18, 2018 at thirteen:31
The need of adding an empty. "" in the line otherstring = otherstring + "" + ch; is totally useless.
Nov three, 2020 at 6:03