Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Last active June 5, 2017 11:46
Show Gist options
  • Save Blasanka/02d3dde1944f69a5d86d2dc713e5d638 to your computer and use it in GitHub Desktop.
Save Blasanka/02d3dde1944f69a5d86d2dc713e5d638 to your computer and use it in GitHub Desktop.
In this code snippet I have taken an example to show you how Java StringTokenizer works.
package stringTokenizer;
import java.util.StringTokenizer;
public class TokenizerExample {
public static void main(String[] args) {
String url = "https//www.facebook.com/slcoders";
//using default delimeter constructor
//StringTokenizer token = new StringTokenizer(names);
//using delimeter constructor
//we can use / \ - . , or even character like
//these are return as only separaters
//StringTokenizer token = new StringTokenizer(names, "A");
//using delimeter and returnValue constructor
//this constructor return as tokens
//above 2 constructors return as only separaters
//for that we have to use third parameter - boolean value true or false
StringTokenizer token = new StringTokenizer(url, "/", true);
//token.countTokens() this will count how many tokens
System.out.println("url tokens " + token.countTokens());
//token.hasMoreTokens() going through the every token in string
while(token.hasMoreTokens()){
//token.nextToken() display the next token
System.out.println(token.nextToken());
}
//If you want this code go to details section below and there is a link
//github or if you have any problem about this comment me
//thank you see you next time
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment