El método java.lang.string.join() concatena los elementos dados con el delimitador y devuelve la string concatenada. Tenga en cuenta que si un elemento es nulo, entonces se agrega nulo. El método join() está incluido en la string java desde JDK 1.8 .
Hay dos tipos de métodos join() en la string java.
Sintaxis:
public static String join(CharSequence deli, CharSequence... ele) and public static String join (CharSequence deli, Iterable<? extends CharSequence> ele) Parameters: deli- delimiter to be attached with each element ele- string or char to be attached with delimiter Returns : string joined with delimiter.
// 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); } }
Producción:
Four < Five < Six < Seven
// Java program to demonstrate // working of join() method class Gfg2 { public static void main(String args[]) { // delimiter is " " and elements are "My", // "name", "is", "Niraj", "Pandey" String gfg2 = String.join(" ", "My", "name", "is", "Niraj", "Pandey"); System.out.println(gfg2); } }
Producción:
My name is Niraj Pandey
// Java program to demonstrate // working of join() method class Gfg3 { public static void main(String args[]) { // delimiter is "->" and elements are "Wake up", // "Eat", "Play", "Sleep", "Wake up" String gfg3 = String.join("-> ", "Wake up", "Eat", "Play", "Sleep", "Wake up"); System.out.println(gfg3); } }
Producción:
Wake up-> Eat-> Play-> Sleep-> Wake up
Publicación traducida automáticamente
Artículo escrito por Niraj_Pandey y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA