go to previous page   go to home page   go to next page
String original = "Mr. Smith was scene walking away from the scene of the crime.";
String fixed    = original.replaceFirst( "scene", "seen" );
System.out.println( fixed );


Mr. Smith was seen walking away from the scene of the crime.


The split() method divides a string into pieces. The pieces of the string are substrings separated by characters that match a pattern. A new String object is created for each piece, and an array is created that holds a reference for each new String.

String[] split( String RE )

For example, this fragment

String manyNumbers = "24 ;  34;  987; 007" ;

String[] pieces = manyNumbers.split( " *; *" );

produces the array

String pieces = {"24", "34", "987", "007"};

The strings in the array are in the same order as they occur in the string that was split.


What array is produced by the following fragment:

String manyAnimals = "ant ,  bat,  cat,   dog" ;

String[] pieces = manyAnimals.split( " *, *");