There are three important methods in StringTokenizer class.
- countTokens() : returns the number of tokens.
- hasMoreTokens() : returns true if more tokens are available .
- nextToken() :returns the current token.
//© http://imagocomputing.blogspot.com/ import java.util.StringTokenizer; class TokenizerDemo { public static void main(String args[]){ String MyStr="Sajith#Nimal#Kamal#Sunil"; StringTokenizer tokens=new StringTokenizer(MyStr,"#"); System.out.println("Real String : " + MyStr); System.out.println("Number of tokens: " + tokens.countTokens()); int i=1; while(tokens.hasMoreTokens()){ System.out.println("Token " + i +" \t: " + tokens.nextToken()); i++; } } }output :
A Practical Example:
//© http://imagocomputing.blogspot.com/ import java.util.StringTokenizer; class GetExtensionDemo { static String getFileExtension(String filePath){ StringTokenizer stk=new StringTokenizer(filePath,"."); String FileExt=""; while(stk.hasMoreTokens()){ FileExt=stk.nextToken(); } return FileExt; } public static void main(String[] args){ String path1="/usr/bin/myProg.pl"; System.out.println("File path : " + path1 + " File extension : " + getFileExtension(path1)); String path2="C:\\about.bmp"; System.out.println("File path : " + path2 + " File extension : " + getFileExtension(path2)); } }Output :
- Since : JDK1.0
0 comments:
Post a Comment