Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

parsing the whole string in seperate word  RSS feed

 
Darshan Bhavsar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have string like
String s="171734---------printer-head.-----No";
i want to break this string into words separeted
with ---
Means i want output as following
word1=171734
word2=---------
word3=printer-head.
word4=-----
word5=No
How could it possible ,i know with StringTokenizer
it is possible but i am not able to get it.
Please help me out.
Any help is appreceated
Thanks in advance
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.javaworld.com/javaworld/javatips/jw-javatip112.html
Check out the url given to find the solution to your problem
-arun
 
Alex Ku
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you be more specific on the format of the string?
[NUMBERs][-s][LETTERs][-s][LETTERs]
and you dont know the size of each token, right?
is the length of the string fixed?
the [-s] is always >= 3?
and in your example, printer-head, has a "-". will it always be like this, 1 "-", or just in some cases.
please be specific.
 
Darshan Bhavsar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply
String format is like
MR_____Has____hand

Underscores are considered as blank
so i want output format like
MR DARSHAN has ONE hand
so underscores token should be replaced with random string .This underscores tokens are not fixed.
So first i want to calculate no of blanks in between the words then replace this blanks with
some predefined String
Thanks once again
[ January 31, 2002: Message edited by: Darshan Bhavsar ]
 
Les Dsouza
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the following form of the string tokenizer -
StringTokenizer(String str, String delim, boolean returnDelims)
If the boolean is true, delimiter characters are themselves considered to be
tokens. A token is either one delimiter character, or a sequence
of consecutive characters that are not delimiters.
StringTokenizer st = new StringTokenizer(inputstring, "-", true);
while (st.hasMoreTokens()) {

\\process tokens
}
Hope this helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!