Refer the first post on String Tokenizer here.
Lets play with a little complicated string in this post! This will just demonstrate how you can use Tokenizer in more often encountered string parsing situations.
Lets run....
Lets play with a little complicated string in this post! This will just demonstrate how you can use Tokenizer in more often encountered string parsing situations.
Lets run....
package
org.chandan.string.tokenizer;
import
java.util.StringTokenizer;
public
class
StringTokenizerSample {
/*
* Notice that, first we need to tokenize
with respect to ';' as
* delimiter.
* Then on generated tokens, we have to again tokenize
* with '=' as delimiter.
*/
private
static
final
String SAMPLE_STRING_1=
"name=myName;address=myAddress;phone=myPhone";
private
static
final
String DELIM_EQUAL="=";
private
static
final
String DELIM_SEMICOLON=";";
public
static
void
main(String args[])
{
System.out.println("PROCESSING
STRING = ["+SAMPLE_STRING_1+"]");
System.out.println("----------------------------------------");
processString(SAMPLE_STRING_1,DELIM_SEMICOLON,DELIM_EQUAL);
System.out.println("");
System.out.println("*********************************************");
}
private
static
void
processString(String sample,String delimiter1
,String
delimiter2)
{
/*
* First tokenize whole
string with respect to first delimeter.
* Then iterate over generated tokens to perform further tokenization.
*/
StringTokenizer
stringTokenizer=new
StringTokenizer(sample,delimiter1);
System.out.println("TOTAL
NO OF MAIN TOKENS FOUND: "
+stringTokenizer.countTokens());
int
tokenCount=1;
while(stringTokenizer.hasMoreElements())
{
String
mainToken=stringTokenizer.nextElement().toString();
System.out.println("");
System.out.println("MAIN
TOKEN["+tokenCount++ +"]
-> "
+mainToken);
System.out.println(".................................");
StringTokenizer
stringTokenizer2=new
StringTokenizer(
mainToken,delimiter2);
System.out.println("");
System.out.println("TOTAL
NO OF SUB TOKENS FOUND: "
+stringTokenizer2.countTokens());
int
subTokenCount=1;
while
(stringTokenizer2.hasMoreElements())
{
System.out.println("SUB
TOKEN["+subTokenCount++ +"]
-> "
+stringTokenizer2.nextElement());
}//END
OF INNER WHILE LOOP
}//END
OF OUTER WHILE LOOP
}//END
OF METHOD
}
OUTPUT:
PROCESSING
STRING = [name=myName;address=myAddress;phone=myPhone]
----------------------------------------
TOTAL
NO OF MAIN TOKENS FOUND: 3
MAIN
TOKEN[1] -> name=myName
.................................
TOTAL
NO OF SUB TOKENS FOUND: 2
SUB
TOKEN[1] -> name
SUB
TOKEN[2] -> myName
MAIN
TOKEN[2] -> address=myAddress
.................................
TOTAL
NO OF SUB TOKENS FOUND: 2
SUB
TOKEN[1] -> address
SUB
TOKEN[2] -> myAddress
MAIN
TOKEN[3] -> phone=myPhone
.................................
TOTAL
NO OF SUB TOKENS FOUND: 2
SUB
TOKEN[1] -> phone
SUB
TOKEN[2] -> myPhone
*********************************************
If you are looking for some tutorials on parsing an xml document, then check HERE.
Happy coding :)
.
If you are looking for some tutorials on parsing an xml document, then check HERE.
Happy coding :)
.
You showed some good code snippets, in my project i need to input the string and make it to parts.Can i use Split() method instead of Tokernizer?
ReplyDeleteYou can!
ReplyDelete+ :if you want to control number of sub strings which will be generated then Split() serves your purpose!
- :Some of the special characters which will be passed as argument to Split() need to be escaped.
More info @ http://www.java-examples.com/java-string-split-example
You have introduced Split() to me :) Thank you!
Hi, You replied on my question at SO about arabic language, could you check my reply please, i even tried with editing the fonts and literally moving the letters downwards but no effect. Kindly send me the file you were using or at least a screen shot... Please i m trying to solve this problem for more than a week...
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi, You replied on my question at SO regarding arabic font in android. Could you please email me the screen shot and font file which you were using because i am trying with different font file but same result. i even edit the font in font editor but no gain. Please reply as i am having this problem for last one week. my email id = farhanahmad123@yahoo.com
ReplyDeleteHello Farhan,
ReplyDeleteshortly you will receive the needed files at your inbox :)
Nice post, I like the example. Also split is very convinient method in Java , though its worth noting that special character like "." or "/" needs to be escaped if used in split.
ReplyDeleteI have also shared my experience as http://javarevisited.blogspot.com/2011/09/string-split-example-in-java-tutorial.html let me know how do you find it.