![]() |
StringTokenizer
Hi
How to read a string, a substring at a time. The StringTokenizer class only breaks strings by 3 delimiters: tab, space, comma. consider StringTokenizer tokeinzer = new StringTokenizer(mystring); while (tokenizer.hasMoreTokens()) { //break string into substrings of 5 characters } Thanks in advance AAa |
Re: StringTokenizer
"Anony!" <someone@something.com> wrote in message
news:M7rNc.18954$K53.11899@news-server.bigpond.net.au... > Hi > How to read a string, a substring at a time. > The StringTokenizer class only breaks strings by 3 delimiters: tab, space, > comma. > consider > StringTokenizer tokeinzer = new StringTokenizer(mystring); > while (tokenizer.hasMoreTokens()) > { > //break string into substrings of 5 characters > } from SUN: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead -- No matter what happens, somebody will find a way to take it too seriously. |
Re: StringTokenizer
"VisionSet" <spam@ntlworld.com> wrote in message news:disNc.377$6p4.207@newsfe1-gui.ntli.net... > > > "Anony!" <someone@something.com> wrote in message > news:M7rNc.18954$K53.11899@news-server.bigpond.net.au... > > Hi > > > > How to read a string, a substring at a time. > > > > The StringTokenizer class only breaks strings by 3 delimiters: tab, space, > > comma. > > No, you can supply any delimiters you want. I tried to figure out how to set a delimiter of 5 characters. Don't think such as a thing exist for delimiters. I may have to go java.io or something aAa |
Re: StringTokenizer
"Anony!" <someone@something.com> wrote in message news:M7rNc.18954$K53.11899@news-server.bigpond.net.au... > Hi > > How to read a string, a substring at a time. > > The StringTokenizer class only breaks strings by 3 delimiters: tab, space, > comma. No, you can supply any delimiters you want. -- Mike W |
Re: StringTokenizer
On Tue, 27 Jul 2004 13:48:59 +0200, "jAnO!"
<j.groot@donotspam.kpn.com> wrote: >"Anony!" <someone@something.com> wrote in message >news:M7rNc.18954$K53.11899@news-server.bigpond.net.au... >> Hi >> How to read a string, a substring at a time. >> The StringTokenizer class only breaks strings by 3 delimiters: tab, >space, >> comma. >> consider >> StringTokenizer tokeinzer = new StringTokenizer(mystring); >> while (tokenizer.hasMoreTokens()) >> { >> //break string into substrings of 5 characters >> } > >from SUN: > >StringTokenizer is a legacy class that is retained for compatibility >reasons although its use is discouraged in new code. It is recommended >that anyone seeking this functionality use the split method of String or >the java.util.regex package instead split() is not approriate to this task, either. If you just want to break the string into five-character chunks, use substring(). |
Re: StringTokenizer
In article <ZvrNc.18984$K53.5115@news-server.bigpond.net.au>,
someone@something.com says... > > "VisionSet" <spam@ntlworld.com> wrote in message > news:disNc.377$6p4.207@newsfe1-gui.ntli.net... > > > > > > "Anony!" <someone@something.com> wrote in message > > news:M7rNc.18954$K53.11899@news-server.bigpond.net.au... > > > Hi > > > > > > How to read a string, a substring at a time. > > > > > > The StringTokenizer class only breaks strings by 3 delimiters: tab, > space, > > > comma. > > > > No, you can supply any delimiters you want. > > I tried to figure out how to set a delimiter of 5 characters. Don't think > such as a thing exist for delimiters. > > I may have to go java.io or something > > aAa > > > As said, you can supply any delimiters. http://java.sun.com/j2se/1.4.2/docs/...Tokenizer.html |
Re: StringTokenizer
As said in other replys u can set any delimiters eg-: new
StringTokenizer(mystring,"a,b,c,d",true); the boolean specifys whether or not the delimiters are returned as tokens. Although i dont think StringTokenizer will do what ure expecting. You could use :- char[] cArray=mystring.toCharArray(); then cast the first 5 chars back into a string, then the next 5 chars, then the next etc... "Anony!" <someone@something.com> wrote in message news:M7rNc.18954$K53.11899@news-server.bigpond.net.au... > Hi > > How to read a string, a substring at a time. > > The StringTokenizer class only breaks strings by 3 delimiters: tab, space, > comma. > > consider > > StringTokenizer tokeinzer = new StringTokenizer(mystring); > > while (tokenizer.hasMoreTokens()) > { > //break string into substrings of 5 characters > } > > Thanks in advance > > AAa > > --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.726 / Virus Database: 481 - Release Date: 22/07/2004 |
Re: StringTokenizer
Anony! <someone@something.com> wrote:
> "VisionSet" <spam@ntlworld.com> wrote in message > news:disNc.377$6p4.207@newsfe1-gui.ntli.net... [Discussion on java.util.StringTokenizer] >> No, you can supply any delimiters you want. > > I tried to figure out how to set a delimiter of 5 characters. Don't think > such as a thing exist for delimiters. Yes, there is. For example the 5 charrcters '1', '2', '3', '4' and '5': StringTokenizer tokenizer = new StringTokenizer ("test", "12345"); > I may have to go java.io or something If you want to split a String into chucks of 5 characters, just use String#substring(int) or String#substring(int, int), as pointed out by Alan Moore. The java.io package is overkill, unless you were using it to read the String's anyway. -- Oscar Kind http://home.hccnet.nl/okind/ Software Developer for contact information, see website PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2 |
Re: StringTokenizer
lets see i have a string
String mystring = "asdfergghjukluipfkjg"; i want to break it up into: asdfe rgghj uklui pfkjg using this: StringTokenizer(mystring,"a,b,c,d,e",true); as u can see the first 5 characters dont match the pattern abcde Another problem is that i cant use substring(startIndex, endIndex) becuase that would be hardcoding it. I want to be able to pass the string through a command-line argument and break the string into substrings of 5 characters in length. I didn't think it would be so damn difficult. Thanks anyway AaA "BADBOY" <badboy_boogey@hotmail.com> wrote in message news:yswNc.516$oa1.486@newsfe5-gui.ntli.net... > As said in other replys u can set any delimiters eg-: new > StringTokenizer(mystring,"a,b,c,d",true); > the boolean specifys whether or not the delimiters are returned as tokens. > Although i dont think StringTokenizer will do what ure expecting. > You could use :- char[] cArray=mystring.toCharArray(); > then cast the first 5 chars back into a string, then the next 5 chars, then > the next etc... > > "Anony!" <someone@something.com> wrote in message > news:M7rNc.18954$K53.11899@news-server.bigpond.net.au... > > Hi > > > > How to read a string, a substring at a time. > > > > The StringTokenizer class only breaks strings by 3 delimiters: tab, space, > > comma. > > > > consider > > > > StringTokenizer tokeinzer = new StringTokenizer(mystring); > > > > while (tokenizer.hasMoreTokens()) > > { > > //break string into substrings of 5 characters > > } > > > > Thanks in advance > > > > AAa > > > > > > > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.726 / Virus Database: 481 - Release Date: 22/07/2004 > > --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.726 / Virus Database: 481 - Release Date: 22/07/2004 |
Re: StringTokenizer
Anony! wrote:
> lets see i have a string > > String mystring = "asdfergghjukluipfkjg"; > > i want to break it up into: > > asdfe > rgghj > uklui > pfkjg > > using this: > > StringTokenizer(mystring,"a,b,c,d,e",true); Sorry, but you need to restate your problem in a way that makes sense. Your declaration of StringTokenizer will break any String whenever it finds a "a" OR a "," (Comma) or a "b" etc. Did you even try this declaration in code? What did you get. If you tokenized your string on a, b, c, d, or e and set returnToken true you'd get: a <- one of the separators s <- between separators d <- another separator f <- between ... e <- another ... rgghjukluipfkjg <-- all the rest Does that help explain what StringTokenizer does ? If you really want groups of exactly 5 characters, it sounds like String.substring is your answer. The suggestion to use char[] sounds a lot like a bad attempt at premature optimization. -Paul |
| All times are GMT. The time now is 04:34 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.