concat two strings code example

Example 1: how to concatenate strings javascript

var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
// does not change the existing strings, but
// returns a new string containing the text
// of the joined strings.

Example 2: string concat javascript

//This method adds two or more strings and returns a new single string.

let str1 = new String( "This is string one" ); 
let str2 = new String( "This is string two" ); 
let str3 = str1.concat(str2.toString());
console.log("str1 + str2 : "+str3)

output:
str1 + str2 : This is string oneThis is string two

Example 3: how add strings together

//Java
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");

Example 4: Write a program to concatenate two strings.

#include <stdio.h>
int main() {
  char s1[100] = "programming ", s2[] = "is awesome";
  int length, j;

  // store length of s1 in the length variable
  length = 0;
  while (s1[length] != '\0') {
    ++length;
  }

  // concatenate s2 to s1
  for (j = 0; s2[j] != '\0'; ++j, ++length) {
    s1[length] = s2[j];
  }

  // terminating the s1 string
  s1[length] = '\0';

  printf("After concatenation: ");
  puts(s1);

  return 0;
}

Example 5: concat no and string in javascript

['Hello', ' ', 'World'].join(''); // 'Hello World'

Example 6: how to add strings together

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");

Tags:

Java Example