📜  StringJoiner 类与 String.join() 在Java连接字符串的方法与示例

📅  最后修改于: 2021-09-15 01:09:01             🧑  作者: Mango

当我们需要连接一组字符串,我们需要手工编写代码,除了这一点,我们需要重复使用的分隔符,有时它会导致一些错误,但是Java的8之后,我们可以使用StringJoiner类在连接字符串和在此之前的Java 8 String.join() 方法那么我们就可以轻松实现我们的目标。

示例:没有 StringJoiner 类和没有 String.join() 方法

方法 :

  • 在这个程序中,我们将不使用Java 8,我们将尝试手动完成它,并尝试了解使用分隔符连接字符串的简单操作,我们需要多少代码/步骤才能实现我们的目标。
  • 首先,我们将加入三个字符串“DSA”、“FAANG”、“ALGO”,分隔符将是“gfg”,为此我们需要在添加下一个字符串之前每次编写分隔符。
  • 接下来,我们将连接包含字符串“DSA”、“FAANG”、“ALGO”和分隔符将是“gfg”的 ArrayList 的元素,为此我们需要遍历 ArrayList,然后使用分隔符添加字符串。
  • 注意这里对于 i=0,我们直接将字符串join2 分配给 ArrayList 的第一个元素,因为如果我们没有放置这个条件,那么我们将得到这样的输出“gfg DSA gfg FAANG gfg ALGO”而实际输出没有ArrayList“DSA gfg FAANG gfg ALGO”的第一个元素之前的分隔符。
  • 从代码中你可以看到为完成这个简单的任务编写了多少代码,现在我们将寻找使用 StringJoiner 类和 String.join() 方法。
Java
// Java program for joining the strings before Java8
// by simple method using '+' to concatenate 
  
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
        // Here we need to join strings "DSA","FAANG","ALGO"
        // and with delimiter " gfg "
        String joined = "DSA"
                        + " gfg "
                        + "FAANG"
                        + " gfg "
                        + "ALGO";
  
        // Here we created an ArrayList of strings
        // ArrayList contains "DSA","FAANG","ALGO"
        ArrayList gfgstrings = new ArrayList<>(
            Arrays.asList("DSA", "FAANG", "ALGO"));
        
        String joined2 = "";
        
        // Now we will iterate over ArrayList
        for (int i = 0; i < gfgstrings.size(); i++) {
            
            // for i== 0 we do not want to add space before
            // first word thats why we checked for i=0
            if (i == 0) {
                joined2 = gfgstrings.get(i);
            }
  
            else {
                joined2
                    = joined2 + " gfg " + gfgstrings.get(i);
            }
        }
        
        // printing the first output
        System.out.println(joined);
        System.out.println("--------------------");
        
        // printing the second output
        System.out.println(joined2);
    }
}


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


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


输出
DSA gfg FAANG gfg ALGO
--------------------
DSA gfg FAANG gfg ALGO

StringJoiner 类

StringJoiner 用于构造由分隔符分隔的字符序列,并且可以选择以提供的前缀开头并以提供的后缀结尾。简单来说,在 StringJoiner 调用的构造函数中我们可以传递三个参数 delimiter、suffix、prefix。前缀添加到字符串的开头,后缀将在末尾。要添加字符串,我们只需调用 StringJoiner 类上的 add() 方法。

句法 :

如果我们有后缀和前缀

StringJoiner joined=StringJoiner(delimiter,prefix,suffix)

joined.add(string1);
joined.add(string2);
joined.add(string3);

如果我们没有后缀和前缀

StringJoiner joined=StringJoiner(delimiter)

joined.add(string1);
joined.add(string2);
joined.add(string3);

方法:

  • 如果要添加一组字符串,则需要创建 StringJoiner 类对象并传递分隔符、前缀、后缀。
  • 这里的分隔符是“gfg”字符串,前缀是“[”,后缀是“]”。现在我们需要使用 add() 方法在 StringJoiner 类中添加字符串。
  • 最后,我们将使用 toString() 方法将 StringJoiner 对象转换为 String 对象。
  • 如果我们有字符串的 ArrayList,分隔符是“gfg”,前缀是“[”,后缀是“]”,我们将遍历 Arraylist 和 StringJoiner 中的元素,然后转换为 String 对象。
  • 最后,我们将打印输出。

使用 StringJoiner 类连接字符串的程序:

Java

// 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 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);
    }
}
输出
[DSA gfg FAANG gfg ALGO]
--------------------
[DSA gfg FAANG gfg ALGO]

String.join() :

Java.lang。 字符串.join() 方法用分隔符连接给定元素并返回连接后的字符串。请注意,如果元素为 null,则添加 null。从 JDK 1.8 开始, Java字符串中引入了 join() 方法。 String.join() 方法接受两个参数,第一个是分隔符,第二个可以是元素或元素的列表或数组,或由 ,(逗号)分隔的元素。

句法 :

在单个元素的情况下

String joined=String.join("delimiter","element1","element2,"element3"...);

在列表的情况下

String joined2=String.join("delimiter",listObject);

返回类型:返回使用分隔符连接的字符串

例子:

方法:使用 String.join() 方法连接字符串

  • 在这个针对单个字符串的示例中,我们需要在 join() 方法中传递分隔符和字符串。这里的分隔符是“gfg”,字符串是“DSA”、“FAANG”、“ALGO”。
  • String.join() 将返回使用分隔符连接的字符串。
  • 对于 ArrayList,我们只需要在连接方法 join(” gfg “,listObject) 中传递分隔符和列表对象,这将返回使用分隔符在 ArrayList 的每个元素上连接的字符串。

Java

// 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 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);
    }
}
输出
DSA gfg FAANG gfg ALGO
--------------------
DSA gfg FAANG gfg ALGO