Split method of the String class

Split method of the String class - Split method of the...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Split method of the String class The split method of the String can be used to extract substrings from a string based on certain delimiters. In the split method, we can specify the string (regular expression) that acts as a delimiters and it returns a set of extracted strings as an array of string. See the following example: public class Split1 { public static void main(String[] args) { String line = "www/asu/edu"; String[] words = new String[3]; //extract sub-strings using "/" as a delimiter, then the split methods returns an array of string. words = line.split("/"); System.out.println("size " + words.length); for (int i=0; i<words.length; i++) System.out.println(words[i] + " with length" + words[i].length()); //Here use are using the sting "www/asu/", thus there is no string to extract after the last "/" //An array containing two strings will be returned. line = "www/asu/"; words = line.split("/"); System.out.println("\nsize " + words.length); for (int i=0; i<words.length; i++) System.out.println(words[i] + " with length" + words[i].length());
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/11/2009 for the course CS 205214 taught by Professor Balasooriya/kouvetakis during the Spring '09 term at ASU.

Page1 / 2

Split method of the String class - Split method of the...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online