• Post Reply Bookmark Topic Watch Topic
  • New Topic

string manipulation  RSS feed

 
fang eve
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a string like this: aaa,bbb,cccc

i want to extract out to be like,
aaa, 1st element
bbb, 2nd element
ccc, 3rd element

of a vector. How should i do it?
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



According to me, I Hope it helps you...

If any concern still then revert me back.


[ July 11, 2006: Message edited by: Ankur Sharma ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Vector vec = new java.util.Vector(java.util.Arrays.asList("aaa,bbb,cccc".split(",")));
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:
java.util.Vector vec = new java.util.Vector(java.util.Arrays.asList("aaa,bbb,cccc".split(",")));


Oops...

I wish I would like to give this solution...

Thanks Michael Dunn

Very smart solution...
 
fang eve
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i understand how split() is being used. I have another problem here. for example my string is aaa,bbb,ccc,aaa.
i want to take out all the elements, but i do not want to have repetition of element.

i want it to be:
aaa
bbb
ccc

the element aaa, only appear once.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a Set.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringTokenizer is only around for legacy issues, and should never be used in new code.
 
fang eve
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you mean? i dont get you, do you have sample codes? How to use a Set?
[ July 11, 2006: Message edited by: fang eve ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Set is an interface that doesn't allow duplicates.

It is part of the Collections API.

If you look in the API at HashSet, you will see an example of a Set.
 
fang eve
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok, thanks

Another question here, i have the string like," a.content", "contents".

i have done a function to remove anything before the full-stop. but however, if i pass in the 2nd string ("contents"), it throws me an error.
How i check the string whether if it contain full-stop?
[ July 11, 2006: Message edited by: fang eve ]
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

have a look at the indexOf function
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!