📜  分流器类 |番石榴 |Java

📅  最后修改于: 2022-05-13 01:55:23.408000             🧑  作者: Mango

分流器类 |番石榴 |Java

Guava 的Splitter 类提供了各种方法来处理对字符串、对象等的拆分操作。它通常通过识别分隔符序列的外观来从输入字符串中提取不重叠的子字符串。此分隔符可以指定为单个字符、固定字符串、正则表达式或 CharMatcher 实例。

声明:以下是com.google.common.base.Splitter类的声明:

@GwtCompatible(emulated = true)
public final class Splitter
   extends Object

下表简要总结了 Guava 的 Splitter 类的方法:

例子:

// Java code to show implementation of
// Guava's Splitter class's method
  
import com.google.common.base.Splitter;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Splitter.on(char separator) returns a splitter
        // that uses the given single-character separator.
           
        // Splitter omitEmptyStrings() omits empty 
        // strings from the results.
        System.out.println(Splitter.on(',')
                  .trimResults()
                  .omitEmptyStrings()
                  .split("GeeksforGeeks ,is, the, 
                     best, website, to, prepare, for, interviews"));
    }
}
输出:
[GeeksforGeeks, is, the, best, website, to, prepare, for, interviews]

Splitter 类提供的其他一些方法是:

例子:

// Java code to show implementation of
// Guava's Splitter class's method
  
import com.google.common.base.Splitter;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // A string variable named str
        String str= "Hello, GFG, What's up ?";
           
        // SplitToList returns a List of the strings. 
        // This can be transformed to an ArrayList or 
        // used directly in a loop.
        List myList = Splitter.on(',').splitToList(str);
           
        for (String ele : myList) {
            System.out.println(ele);
        }
    }
}
输出:
Hello
 GFG
 What's up ?

参考:谷歌番石榴