xxxxxxxxxx
String a = "Hello";
String b = " World";
String c = a+b;
xxxxxxxxxx
String.join(" - ", "This", "course", "is", "awesome");
// ou
String.join(" - ", new String[]{"This","course","is","awesome"});
// retourne "This - course - is - awesome"
String.valueOf(22); // retourne "22"
xxxxxxxxxx
// Java program to demonstrate
// working of join() method
class Gfg1 {
public static void main(String args[])
{
// delimiter is "<" and elements are "Four", "Five", "Six", "Seven"
String gfg1 = String.join(" < ", "Four", "Five", "Six", "Seven");
System.out.println(gfg1);
}
}
xxxxxxxxxx
// Java program for joining the strings
// Using String.join() method
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringJoiner;
class GFG {
public static void main(String[] args)
{
// Here we need to join strings "DSA","FAANG","ALGO"
// and with delimiter " gfg "
String joined
= String.join(" gfg ", "DSA", "FAANG", "ALGO");
// Here we created an ArrayList of strings
ArrayList<String> gfgstrings = new ArrayList<>(
Arrays.asList("DSA", "FAANG", "ALGO"));
String joined2 = String.join(" gfg ", gfgstrings);
// printing the first output
System.out.println(joined);
System.out.println("--------------------");
// printing the second output
System.out.println(joined2);
}
}
xxxxxxxxxx
class Main {
public static void main(String[] args) {
// create first string
String first = "Java ";
System.out.println("First String: " + first);
// create second
String second = "Programming";
System.out.println("Second String: " + second);
// join two strings
String joinedString = first.concat(second);
System.out.println("Joined String: " + joinedString);
}
}
xxxxxxxxxx
// Java program for joining the strings
// Using StringJoiner class
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringJoiner;
class GFG {
public static void main(String[] args)
{
// Here we need to join strings "DSA","FAANG","ALGO"
// and with delimiter " gfg "
StringJoiner sj
= new StringJoiner(" gfg ", "[", "]");
sj.add("DSA");
sj.add("FAANG");
sj.add("ALGO");
// Convert StringJoiner to String
String joined = sj.toString();
// Here we created an ArrayList of strings
ArrayList<String> gfgstrings = new ArrayList<>(
Arrays.asList("DSA", "FAANG", "ALGO"));
// StringJoiner class having parameters
// delimiter,prefix,suffix
StringJoiner sj2
= new StringJoiner(" gfg ", "[", "]");
// Now we will iterate over ArrayList
// and adding in string Joiner class
for (String arl : gfgstrings) {
sj2.add(arl);
}
// convert StringJoiner object to String
String joined2 = sj2.toString();
// printing the first output
System.out.println(joined);
System.out.println("--------------------");
// printing the second output
System.out.println(joined2);
}
}