Wednesday, July 27, 2016

How to do split the String using more than one delimiter

How to do split the String using more than one delimiter

Split the String using more than one delimiter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
       package com.javasunderesan;

public class SplitStringDelimitters {

 public static void main(String[] args) {

  String str = "name Java Sunderesan field computer engineering grade 9.98";
  try {
   String[] splits = str.split("name |field |grade ");
   for (int i = 1; i < splits.length; i++) {
    System.out.println(splits[i]);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

}

Output::
Java Sunderesan
computer engineering
9.98


Java - String split() Method

Description:

This method has two variants and splits this string around matches of the given regular expression.


Syntax:
Here is the syntax of this method:

public String[] split(String regex, int limit)
or
public String[] split(String regex)


Parameter

regex : regular expression to be applied on string.

limit : limit for the number of strings in array. If it is zero, it will returns all the strings matching regex.Parameter


Returns

array of strings



Throws

PatternSyntaxException if pattern for regular expression is invalid



No comments :

Post a Comment