Java: How to split Java String with delimeter comma, space, new line, tab ? [SOLVED]


Sometimes it is very important to split a string on a comma \n \t etc. This piece of code will help you with it.


import java.util.*;

 public class split{

  public  static void main ( String args[]) {
       // this is my string
	String s = "Java,	Programming	is ,,, fun do you agree? ."; split
	String regexp = "[\\s,;\\n\\t]+"; // these are my delimiters
	String [] tokens; // here i will save tokens
	// length is 7 because i know that my string will consist of 7 tokens
	for(int i=0;i<7;i++){
		tokens = s.split(regexp);
		System.out.println(tokens[i]);
	}
   }
}

 // tokens are: Java Programming is fun do you agree?

Check out more posts on “Useful Java Functions and Tutorials”

Advertisements

About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module, Red Hat and some other places :) I am an author of the book 'Eclipse Debugging How To', Muay Thai fighter and amateur photographer ;)
This entry was posted in Java, open-source, Useful Functions and tagged , , , , , , , , , , , , . Bookmark the permalink.

10 Responses to Java: How to split Java String with delimeter comma, space, new line, tab ? [SOLVED]

  1. jetcracker says:

    >> // length is 7 because i know that my string will consist of 7 tokens
    We never know =)

  2. b2b says:

    use Apache Commons StringUtils.split(String text, String delimeters) dude and dont build own bicycle

  3. b2b thanks for sharing !

    I don’t think that creating 18-line function is reinventing the wheel, its purpose is to show alternative way of solving the current problem.

    This function can be customized for any language that accepts regular expressions, unfortunately StringUtils is language-limited.

  4. Howdy! I simply want to give you a big thumbs up for the excellent information you have
    right here on this post. I will be coming back to your web
    site for more soon.

  5. Paying subscribers are charged £10 a month however then can use Spotify
    wherever, including offline on mobile devices,
    and don’t should listen to adverts.

  6. Free Slots says:

    I don’t know whether it’s just me or if everyone
    else experiencing issues with your blog. It appears as if
    some of the written text within your posts are running off the
    screen. Can someone else please comment and let me know if this is
    happening to them as well? This could be a problem with
    my browser because I’ve had this happen previously.

    Cheers

  7. stuff says:

    you need to move the line that calls split outside the for loop. As it is you split the line 7 times, when you only need to do it once.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s