• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to split String in Java  RSS feed

 
Kiran Er
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to split the string in java to split the values

String ex:  I27829 ram@gmail.com,H28191 jessica@gmail.com,K29181 jerry@gmail.com.

I want to get the values I27829,H28191,K29181

Can anyone post me sample program for this

Thanks,
Kiran
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kiran!,Welcome to JavaRanch.
did you give it a try?
if not,i am providing you a hint:check the split method in the String class or as an alternate option you can use a StringBuilder class.if you are not familiar with Regular expression(Regex),go with the latter opinion.
While implementing,if you get some problem then don't shy to share your code with us.

Hope this will help!
Kind Regards,
Praveen.
 
Kiran Er
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code:



I am getting output like this : [I4227611, M2289, L399488]. But i don't want the beginning character '[' and end character ']' . How do i achieve this. Because i need concat this with other string in my other program.
 
Kiran Er
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestion. I was able to achieve with Stringbuilder.

 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiran,next time please use code tags.that will make your code look better and readable.
you are printing the array list,whenever you do so you will get the stuff printed like this-"[toString(element@index1),toString(element@index2),so on....]",square brackets are default.if you really don't need them then you can iterate through the list and print each element.if you would mention your concat policy i.e..how you really want to concat the above string in another program,i will better help you.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kiran, I've fixed the code tags for you. To use code tags you must highlight the code you want to be enclosed in code tags before pressing the code tag button.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

What you are passing as an argument to the split() method is a regular expression. Try splitting with "[, ]" or even "[,\\s+]" which will accept comma‑or‑one‑space or comma‑or‑any‑number‑of‑whitespace‑characters. You may find that only the even‑numbered elements of the array match your requirements.

Don't use copy‑and‑paste on this post.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiran Er wrote:. . .
That looks like something which a StringJoiner object would handle even better. Note: Java8 class only.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!