Peter Wank
6
Q:

java concatenate strings

public class ConcatenationExample {
   public static void main(String args[]) {
       //One way of doing concatenation
       String str1 = "Welcome";
       str1 = str1.concat(" to ");
       str1 = str1.concat(" String handling ");
       System.out.println(str1);

       //Other way of doing concatenation in one line
       String str2 = "This";
       str2 = str2.concat(" is").concat(" just a").concat(" String");
       System.out.println(str2);
   }
}
1
public class Concat {
    String cat(String a, String b) {
        a += b;
        return a;
    }
}
2
// StringBuilder
String stringBuilderConcat = new StringBuilder()
    .append(greeting)
    .append(" ")
    .append(person)
    .append("! Welcome to the ")
    .append(location)
    .append("!")
    .build();
0
String firstName = "BarackObama";
String lastName = " Care";
//First way
System.out.println(firstName + lastName);
//Second way
String name = firstName + lastName;
System.out.println(name);
//Third way
System.out.println("BarackObama" + " Care");
-1
class Main
{
	public static void main(String args[])
    {
		String myString = "";
      	for (int i = 0; i < 3; i++)
        {
        	myString += "Hello There "; 
        }
      	System.out.println(myString); // Hello There Hello There Hello There 
    }
}
1
// CPP program to concatenate 
// two integers into one 
  
#include <iostream> 
#include <string> 
using namespace std; 
  
// Function to concatenate 
// two integers into one 
int concat(int a, int b) 
{ 
  
    // Convert both the integers to string 
    string s1 = to_string(a); 
    string s2 = to_string(b); 
  
    // Concatenate both strings 
    string s = s1 + s2; 
  
    // Convert the concatenated string 
    // to integer 
    int c = stoi(s); 
  
    // return the formed integer 
    return c; 
} 
  
int main() 
{ 
    int a = 23; 
    int b = 43; 
  
    cout << concat(a, b) << endl; 
  
    return 0; 
} 
  
// This code is contributed by Ayush Singla(@ayusin51) 
0

New to Communities?

Join the community