Sunday 9 August 2015

Guava: Splitter: Splits string using a separator


By using Splitter class, you can split a string using a separator. The separator can be a character, string, regular expression or CharMatcher instance.

Let’s start with simple example.

import com.google.common.base.Splitter;

public class SplitterEx {
  public static void main(String args[]) {
    Splitter splitter = Splitter.on("$");

    Iterable<String> result = splitter
        .split("English$Hindi$Telugu$Kananda");

    for (String str : result) {
      System.out.println(str);
    }

  }
}


Output
English
Hindi
Telugu
Kananda

Splitter.on("$");
Above statement creates Splitter instance and use ‘$’ as separator.

splitter.split("English$Hindi$Telugu$Kananda");
Above method split string using separator ‘$’.
You can trim the result and omit empty strings.

import com.google.common.base.Splitter;

public class SplitterEx {

  public static void main(String args[]) {
    Splitter splitter1 = Splitter.on("$");
    Splitter splitter2 = Splitter.on("$").omitEmptyStrings();
    Splitter splitter3 = Splitter.on("$").trimResults();
    Splitter splitter4 = Splitter.on("$").trimResults().omitEmptyStrings();

    String str = " English   $ $ $ $ Hindi $ $ $Telugu$Kananda ";

    System.out.println("Simple split");
    Iterable<String> result = splitter1.split(str);
    System.out.println(result);

    System.out.println("Omitting empty strings");
    result = splitter2.split(str);
    System.out.println(result);

    System.out.println("Trimming results");
    result = splitter3.split(str);
    System.out.println(result);

    System.out.println("By omitting and trimming results");
    result = splitter4.split(str);
    System.out.println(result);
  }
}


Output

Simple split
[ English   ,  ,  ,  ,  Hindi ,  ,  , Telugu, Kananda ]
Omitting empty strings
[ English   ,  ,  ,  ,  Hindi ,  ,  , Telugu, Kananda ]
Trimming results
[English, , , , Hindi, , , Telugu, Kananda]
By omitting and trimming results
[English, Hindi, Telugu, Kananda]

Stop splitting after reaching certain limit

You can stop splitting after reaches certain limit using “limit” function.

import com.google.common.base.Splitter;

public class SplitterEx {

  public static void main(String args[]) {
    Splitter splitter = Splitter.on("$").limit(4);
    String str = "Hari$Krishna$Vadiraj$Sunil$Sankalp$abcd$qwer";

    System.out.println(splitter.split(str));
  }
}


Output

[Hari, Krishna, Vadiraj, Sunil$Sankalp$abcd$qwer]

Split data to list

You can store the split result into a List using splitToList method.

import java.util.List;

import com.google.common.base.Splitter;

public class SplitterEx {

  public static void main(String args[]) {
    Splitter splitter = Splitter.on("$");
    String str = "Hari$Krishna$Vadiraj$Sunil$Sankalp$Phalgun$Sambith";

    List<String> employees = splitter.splitToList(str);

    for (String emp : employees) {
      System.out.println(emp);
    }
  }
}


Output

Hari
Krishna
Vadiraj
Sunil
Sankalp
Phalgun
Sambith


Splitter class has MapSpliiter class, which takes a string in which keys and values are delimited by one separator, and key-value pair is delimited by another separator and returns a map instance.

import java.util.Map;
import java.util.Set;

import com.google.common.base.Splitter;
import com.google.common.base.Splitter.MapSplitter;

public class SplitterEx {

  public static void main(String args[]) {
    String str = "1=Hari Krishna Gurram$2=Arpan Debroy$3=Sujatha$4=Preethi Nair";

    Splitter splitter = Splitter.on("$");

    MapSplitter mapSplitter = splitter.withKeyValueSeparator('=');

    Map<String, String> employees = mapSplitter.split(str);

    Set<String> ids = employees.keySet();

    for (String id : ids) {
      System.out.println(id + "\t" + employees.get(id));
    }

  }
}


Output

1  Hari Krishna Gurram
2 Arpan Debroy
3 Sujatha
4 Preethi Nair










Prevoius                                                 Next                                                 Home

No comments:

Post a Comment