Java examples for Language Basics:StringTokenizer
Reverse string by word using StringTokenizer
import java.util.StringTokenizer; public class Main { public static void main(String[] args) { String strLine = "Java Reverse string by word example"; //specify delimiter as " " space StringTokenizer st = new StringTokenizer(strLine, " "); //w w w . j a v a 2 s . c om String strReversedLine = ""; while(st.hasMoreTokens()) { strReversedLine = st.nextToken() + " " + strReversedLine; } System.out.println("Reversed string by word is : " + strReversedLine); } }