Can you define what you mean by a special char? Do you mean "only alphanumeric characters"?
If you want to remove all possible special characters and leave only alphanumeric characters, then you can use the following:
s = s.replaceAll("[^a-zA-Z0-9]", "");
If you want to print them one by one:
s = s.replaceAll("[^a-zA-Z0-9]", "/");
String[] splitString = s.split("/");
for(String str:splitString)
{
System.out.println(str);
}
EIDT:
Since you asked: the above code uses the enhanced for loop (also called the for-each loop) introduced by java 5.
Using the 'normal' for loop, this is:
s = s.replaceAll("[^a-zA-Z0-9]", "/");
String[] splitString = s.split("/");
for(int i=0; i<splitString.length(); i++)
{
String str = splitString[i];
System.out.println(str);
}
Also:
StringTokenizer
is a legacy class
that is retained for compatibility
reasons although its use is
discouraged in new code. It is
recommended that anyone seeking this
functionality use the split
method
of String
or the java.util.regex
package instead.
(from the javadoc)