Uselesssss
Uselesssss

Reputation: 2133

String Tokenizer, delimiter

I am using this code:

StringTokenizer tokenizer=new StringTokenizer(line, "::");

to split the following String:

hi my name is visghal:: what is yor name name:: being thw simple my::: what is yor name name.

Now i want to split the string using :: as delimiter. It is working fine. But it is also taking ::: into consideration.

In other words i want:

hi my name is visghal
what is yor name name
being thw simple my
: what is yor name name

Instead it is giving me the following:

being thw simple my 
what is yor name name   
hi my name is visghal   

It is taking :: and ::: as same. Is there any means to avoid this?

Upvotes: 2

Views: 5714

Answers (3)

Azodious
Azodious

Reputation: 13872

It is taking :: and ::: as same

No, your delimeter :: is found twice in this string part ::: and this is the explanation for your result.

You should use split("::") method.

Upvotes: 0

John B
John B

Reputation: 32949

Try Guava's Splitter if you need additional functionality over String.split. It will allow for trimming and omitting empty strings.

 String myInput = "...";
 Iterable<String> parts = Splitter.on("::").split(myInput);

Upvotes: 2

anubhava
anubhava

Reputation: 785008

You can just use String#split like this:

String[] arr = str.split("::");

EDIT:

String[] arr = str.split("::\\s*"); // for stripping spaces after ::

OUTPUT:

hi my name is visghal
what is yor name name
being thw simple my
: what is yor name name

Upvotes: 6

Related Questions