HI all,

apologies for the strange username!

I have a string as follows:

13866,Stephen Moran,Stephen Moran,Killaroo,Streamstown,Westmeath,Ireland,,Moran ,2010-08-05 00:00:00,0,0,,,0,O4,Moran_013866

There are 17 place holders in the string, sometimes they will be empty,sometimes not.

When I tokenise it , the blanks are being ignored and its doesn't return 17 tokens.

Is there anyway I can get it to return the token blank or not?

many thanks

Redbyte

Recommended Answers

All 4 Replies

Have you tried using the third constructor for the StringTokenizer class?

the blanks are being ignored

Do you mean empty tokens? I think of a blank as being the same as a space character.
Ie empty = "", space = " "

Have you tried using the third constructor for the StringTokenizer class?


Do you mean empty tokens? I think of a blank as being the same as a space character.
Ie empty = "", space = " "

Hi Norm,

I mean empty as in "".

Im looking at String [] split = fileName.split(",");

this doesnt ignore the blanks, but leaves my post processing messy. I was originally adding my Tokenized string to an array for later processing of content.

Thanks

RB

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.

Source: JDK 6 API http://download.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html
String's split method will return what you want:

String s = "13866,Stephen Moran,Stephen Moran,Killaroo,Streamstown,Westmeath,Ireland,,Moran ,2010-08-05 00:00:00,0,0,,,0,O4,Moran_013866";
String[] tokens = s.split(",");
System.out.println(tokens.length);

>> 17, and is NOT "discouraged"

EDIT: parallel post with previous update. Why "messy" if you were going to put them in an array anyway?

EDIT: parallel post with previous update. Why "messy" if you were going to put them in an array anyway?

Thanks for confirming what I was thinking!! Messy, as I have to rejig my code and its Friday afternoon, and I'm not really a Java developer!!

Thanks all

Cheers

RB

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.