[java] How to concatenate characters in java?

How do you concatenate characters in java? Concatenating strings would only require a + between the strings, but concatenating chars using + will change the value of the char into ascii and hence giving a numerical output. I want to do System.out.println(char1+char2+char3... and create a String word like this.

I could do

System.out.print(char1);
System.out.print(char2);
System.out.print(char3);

But, this will only get me the characters in 1 line. I need it as a string. Any help would be appreciated.

Thanks

This question is related to java character concatenation

The answer is


You can use the String constructor.

System.out.println(new String(new char[]{a,b,c}));

public class initials {

public static void main (String [] args) {

    char initialA = 'M';
    char initialB = 'P';
    char initialC = 'T';

    System.out.println("" + initialA + initialB + initialC );


}   

}


If you have a bunch of chars and want to concat them into a string, why not do

System.out.println("" + char1 + char2 + char3); 

?


this is very simple approach to concatenate or append the character

       StringBuilder desc = new StringBuilder(); 
       String Description="this is my land"; 
       desc=desc.append(Description.charAt(i));

Do you want to make a string out of them?

String s = new StringBuilder().append(char1).append(char2).append(char3).toString();

Note that

String b = "b";
String s = "a" + b + "c";

Actually compiles to

String s = new StringBuilder("a").append(b).append("c").toString();

Edit: as litb pointed out, you can also do this:

"" + char1 + char2 + char3;

That compiles to the following:

new StringBuilder().append("").append(c).append(c1).append(c2).toString();

Edit (2): Corrected string append comparison since, as cletus points out, a series of strings is handled by the compiler.

The purpose of the above is to illustrate what the compiler does, not to tell you what you should do.


simple example to selecting character from string and appending to string variable

private static String findaccountnum(String holdername, String mobile) {

    char n1=holdername.charAt(0);
    char n2=holdername.charAt(1);
    char n3=holdername.charAt(2);
    char n4=mobile.charAt(0);
    char n5=mobile.charAt(1);
    char n6=mobile.charAt(2);
    String number=new StringBuilder().append(n1).append(n2).append(n3).append(n4).append(n5).append(n6).toString();

    return number;
}

You need a String object of some description to hold your array of concatenated chars, since the char type will hold only a single character. e.g.,

StringBuilder sb = new StringBuilder('a').append('b').append('c');
System.out.println(sb.toString);

Do you want to make a string out of them?

String s = new StringBuilder().append(char1).append(char2).append(char3).toString();

Note that

String b = "b";
String s = "a" + b + "c";

Actually compiles to

String s = new StringBuilder("a").append(b).append("c").toString();

Edit: as litb pointed out, you can also do this:

"" + char1 + char2 + char3;

That compiles to the following:

new StringBuilder().append("").append(c).append(c1).append(c2).toString();

Edit (2): Corrected string append comparison since, as cletus points out, a series of strings is handled by the compiler.

The purpose of the above is to illustrate what the compiler does, not to tell you what you should do.


You need to tell the compiler you want to do String concatenation by starting the sequence with a string, even an empty one. Like so:

System.out.println("" + char1 + char2 + char3...);

I don't really consider myself a Java programmer, but just thought I'd add it here "for completeness"; using the (C-inspired) String.format static method:

String s = String.format("%s%s", 'a', 'b'); // s is "ab"

You need a String object of some description to hold your array of concatenated chars, since the char type will hold only a single character. e.g.,

StringBuilder sb = new StringBuilder('a').append('b').append('c');
System.out.println(sb.toString);

System.out.print(a + "" + b + "" + c);

You can use the String constructor.

System.out.println(new String(new char[]{a,b,c}));

I don't really consider myself a Java programmer, but just thought I'd add it here "for completeness"; using the (C-inspired) String.format static method:

String s = String.format("%s%s", 'a', 'b'); // s is "ab"

simple example to selecting character from string and appending to string variable

private static String findaccountnum(String holdername, String mobile) {

    char n1=holdername.charAt(0);
    char n2=holdername.charAt(1);
    char n3=holdername.charAt(2);
    char n4=mobile.charAt(0);
    char n5=mobile.charAt(1);
    char n6=mobile.charAt(2);
    String number=new StringBuilder().append(n1).append(n2).append(n3).append(n4).append(n5).append(n6).toString();

    return number;
}

System.out.println(char1+""+char2+char3)

or

String s = char1+""+char2+char3;

If you have a bunch of chars and want to concat them into a string, why not do

System.out.println("" + char1 + char2 + char3); 

?


System.out.println(char1+""+char2+char3)

or

String s = char1+""+char2+char3;

You need to tell the compiler you want to do String concatenation by starting the sequence with a string, even an empty one. Like so:

System.out.println("" + char1 + char2 + char3...);

You can use the String constructor.

System.out.println(new String(new char[]{a,b,c}));

You need a String object of some description to hold your array of concatenated chars, since the char type will hold only a single character. e.g.,

StringBuilder sb = new StringBuilder('a').append('b').append('c');
System.out.println(sb.toString);

You need to tell the compiler you want to do String concatenation by starting the sequence with a string, even an empty one. Like so:

System.out.println("" + char1 + char2 + char3...);

If you have a bunch of chars and want to concat them into a string, why not do

System.out.println("" + char1 + char2 + char3); 

?


I wasn't going to answer this question but there are two answers here (that are getting voted up!) that are just plain wrong. Consider these expressions:

String a = "a" + "b" + "c";
String b = System.getProperty("blah") + "b";

The first is evaluated at compile-time. The second is evaluated at run-time.

So never replace constant concatenations (of any type) with StringBuilder, StringBuffer or the like. Only use those where variables are invovled and generally only when you're appending a lot of operands or you're appending in a loop.

If the characters are constant, this is fine:

String s = "" + 'a' + 'b' + 'c';

If however they aren't, consider this:

String concat(char... chars) {
  if (chars.length == 0) {
    return "";
  }
  StringBuilder s = new StringBuilder(chars.length);
  for (char c : chars) {
    s.append(c);
  }
  return s.toString();
}

as an appropriate solution.

However some might be tempted to optimise:

String s = "Name: '" + name + "'"; // String name;

into this:

String s = new StringBuilder().append("Name: ").append(name).append("'").toString();

While this is well-intentioned, the bottom line is DON'T.

Why? As another answer correctly pointed out: the compiler does this for you. So in doing it yourself, you're not allowing the compiler to optimise the code or not depending if its a good idea, the code is harder to read and its unnecessarily complicated.

For low-level optimisation the compiler is better at optimising code than you are.

Let the compiler do its job. In this case the worst case scenario is that the compiler implicitly changes your code to exactly what you wrote. Concatenating 2-3 Strings might be more efficient than constructing a StringBuilder so it might be better to leave it as is. The compiler knows whats best in this regard.


Do you want to make a string out of them?

String s = new StringBuilder().append(char1).append(char2).append(char3).toString();

Note that

String b = "b";
String s = "a" + b + "c";

Actually compiles to

String s = new StringBuilder("a").append(b).append("c").toString();

Edit: as litb pointed out, you can also do this:

"" + char1 + char2 + char3;

That compiles to the following:

new StringBuilder().append("").append(c).append(c1).append(c2).toString();

Edit (2): Corrected string append comparison since, as cletus points out, a series of strings is handled by the compiler.

The purpose of the above is to illustrate what the compiler does, not to tell you what you should do.


System.out.print(a + "" + b + "" + c);

You need a String object of some description to hold your array of concatenated chars, since the char type will hold only a single character. e.g.,

StringBuilder sb = new StringBuilder('a').append('b').append('c');
System.out.println(sb.toString);

Do you want to make a string out of them?

String s = new StringBuilder().append(char1).append(char2).append(char3).toString();

Note that

String b = "b";
String s = "a" + b + "c";

Actually compiles to

String s = new StringBuilder("a").append(b).append("c").toString();

Edit: as litb pointed out, you can also do this:

"" + char1 + char2 + char3;

That compiles to the following:

new StringBuilder().append("").append(c).append(c1).append(c2).toString();

Edit (2): Corrected string append comparison since, as cletus points out, a series of strings is handled by the compiler.

The purpose of the above is to illustrate what the compiler does, not to tell you what you should do.


I wasn't going to answer this question but there are two answers here (that are getting voted up!) that are just plain wrong. Consider these expressions:

String a = "a" + "b" + "c";
String b = System.getProperty("blah") + "b";

The first is evaluated at compile-time. The second is evaluated at run-time.

So never replace constant concatenations (of any type) with StringBuilder, StringBuffer or the like. Only use those where variables are invovled and generally only when you're appending a lot of operands or you're appending in a loop.

If the characters are constant, this is fine:

String s = "" + 'a' + 'b' + 'c';

If however they aren't, consider this:

String concat(char... chars) {
  if (chars.length == 0) {
    return "";
  }
  StringBuilder s = new StringBuilder(chars.length);
  for (char c : chars) {
    s.append(c);
  }
  return s.toString();
}

as an appropriate solution.

However some might be tempted to optimise:

String s = "Name: '" + name + "'"; // String name;

into this:

String s = new StringBuilder().append("Name: ").append(name).append("'").toString();

While this is well-intentioned, the bottom line is DON'T.

Why? As another answer correctly pointed out: the compiler does this for you. So in doing it yourself, you're not allowing the compiler to optimise the code or not depending if its a good idea, the code is harder to read and its unnecessarily complicated.

For low-level optimisation the compiler is better at optimising code than you are.

Let the compiler do its job. In this case the worst case scenario is that the compiler implicitly changes your code to exactly what you wrote. Concatenating 2-3 Strings might be more efficient than constructing a StringBuilder so it might be better to leave it as is. The compiler knows whats best in this regard.


You can use the String constructor.

System.out.println(new String(new char[]{a,b,c}));

this is very simple approach to concatenate or append the character

       StringBuilder desc = new StringBuilder(); 
       String Description="this is my land"; 
       desc=desc.append(Description.charAt(i));

public class initials {

public static void main (String [] args) {

    char initialA = 'M';
    char initialB = 'P';
    char initialC = 'T';

    System.out.println("" + initialA + initialB + initialC );


}   

}


I wasn't going to answer this question but there are two answers here (that are getting voted up!) that are just plain wrong. Consider these expressions:

String a = "a" + "b" + "c";
String b = System.getProperty("blah") + "b";

The first is evaluated at compile-time. The second is evaluated at run-time.

So never replace constant concatenations (of any type) with StringBuilder, StringBuffer or the like. Only use those where variables are invovled and generally only when you're appending a lot of operands or you're appending in a loop.

If the characters are constant, this is fine:

String s = "" + 'a' + 'b' + 'c';

If however they aren't, consider this:

String concat(char... chars) {
  if (chars.length == 0) {
    return "";
  }
  StringBuilder s = new StringBuilder(chars.length);
  for (char c : chars) {
    s.append(c);
  }
  return s.toString();
}

as an appropriate solution.

However some might be tempted to optimise:

String s = "Name: '" + name + "'"; // String name;

into this:

String s = new StringBuilder().append("Name: ").append(name).append("'").toString();

While this is well-intentioned, the bottom line is DON'T.

Why? As another answer correctly pointed out: the compiler does this for you. So in doing it yourself, you're not allowing the compiler to optimise the code or not depending if its a good idea, the code is harder to read and its unnecessarily complicated.

For low-level optimisation the compiler is better at optimising code than you are.

Let the compiler do its job. In this case the worst case scenario is that the compiler implicitly changes your code to exactly what you wrote. Concatenating 2-3 Strings might be more efficient than constructing a StringBuilder so it might be better to leave it as is. The compiler knows whats best in this regard.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to character

Set the maximum character length of a UITextField in Swift Max length UITextField Remove last character from string. Swift language Get nth character of a string in Swift programming language How many characters can you store with 1 byte? How to convert integers to characters in C? Converting characters to integers in Java How to check the first character in a string in Bash or UNIX shell? Invisible characters - ASCII How to delete Certain Characters in a excel 2010 cell

Examples related to concatenation

Pandas Merging 101 What does ${} (dollar sign and curly braces) mean in a string in Javascript? Concatenate two NumPy arrays vertically Import multiple csv files into pandas and concatenate into one DataFrame How to concatenate columns in a Postgres SELECT? Concatenate string with field value in MySQL Most efficient way to concatenate strings in JavaScript? How to force a line break on a Javascript concatenated string? How to concatenate two IEnumerable<T> into a new IEnumerable<T>? How to concat two ArrayLists?