Splitting up a string - Java Language Basics

Java examples for Language Basics:String

Description

Splitting up a string

Demo Code

public class Main {
  public static void main(String[] argv) {
    String address = "1500 N. Main Street:L.A.:CA:93722";
    String[] parts = address.split(":");
    for (int i = 0; i < parts.length; i++)
      System.out.println(parts[i]);
  }//from   ww  w . j a  v  a 2 s.c  om
}

Here are a few regular expressions that might be useful when you use the split method:

Regular ExpressionExplanation
\\t A tab character
\\n A newline character
\\| A vertical bar
\\s Any white space character
\\s+ One or more occurrences of any white space character

Related Tutorials