public class StrSamples
{
public static void main (String args [])
{
String s ="This is a sample string";
System.out.println(s);
System.out.println("It contains this many characters (counting spaces): "+ s.length());
System.out.println("Here it is split up by whitespace:");
String [] seperated= s.split("\\s+");
for(int i=0; i<seperated.length; i++)
{
System.out.println(seperated[i]);
}
System.out.println("Here it is split up by the letter a:");
String [] sep2= s.split("a");
for(int i=0; i<sep2.length; i++)
{
System.out.println(sep2[i]);
}
System.out.println("Here are the characters from position 1 to 3:");
System.out.println(s.substring(1,3));
System.out.println("Here it is with the spaces removed");
String r= s.replaceAll("\\s", "");
System.out.println(r);
System.out.println("Here it is with the letter s replaced by sausage");
r= s.replaceAll("s", "sausage");
System.out.println(r);
System.out.println("Here it is with the lower case letters turned to *s");
r= s.replaceAll("[a-z]", "*");
System.out.println(r);
}
}