Java Split Method

What Is Java Split Method?

programming Java, Java Split Method
Programming Java

There’s a method in java that is used to “split” string into pieces, the split is based on a given regular expressions and a return char array. There are 2 type of syntax that can be used for Java Split Method, (for a fun fact java split method is available since java 1.4).


first :

public String split(String regex)  

second :

public String split(String regex, int limit)  

Split() Method Syntax


To get this to work, you need to be able to fill the parameter of the method syntax. Here’s a clue :

  • Regex : Strings need to apply regular expression
  • Limit :  The limit for how much number of strings in array. If it is zero, it will returns all the strings matching regex.

Incase something went wrong, and you need to know what is wrong, you could implements a Throws method, like this one:

PatternSyntaxException (You could use this if the regular expression is invalid)

Split() Method Example

Here is one example of the method :

public class SplitExmpl{  

public static void main(String args[]){

 String s1=”java string split method by craftingez6″;
 String[] words=s1.split(“\s”);//splits the string based on whitespace
  //using java foreach loop to print elements of string array
  for(String w:words){
    System.out.println(w);
}
}}

This will output :

java

string

split

method

by

craftingez6

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

One thought on “Java Split Method”

  1. After I originally commented I clicked the -Notify me when new feedback are added- checkbox and now each time a comment is added I get four emails with the identical comment. Is there any manner you possibly can remove me from that service? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.