Solved

Spliting a string word into segments

Posted on 2007-03-24
6
190 Views
Last Modified: 2010-03-31
I am trying to spilt a word into segments whenever the char "," occurs in the word.
For example,
String name= "[John, David]"
So the two segments should be John and David, which ignore the following
" [ "
" ] "
 " , "
This can work if I use substring, but I have to have a fixed amount of words, but the number of words will be dynamic.
I can do this with seperate chars and store each segment into an array, but I need help doing it with words.


 
0
Comment
Question by:mr_usmannaeem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 18785146
String[] nameParts = name.split("[\\[\\,]]");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18785148
Typo sorry


String[] nameParts = name.split("[\\[\\],]");
0
 
LVL 4

Expert Comment

by:MacroLand
ID: 18785417
You can also use StringTokenizer class to parse your string into tokens by a specified delimiter, in your case it is comma.

Regards,
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mr_usmannaeem
ID: 18785418
public class split{
   public static void main(String[] args) throws Exception{
   String combo="[ABC, B]";
   System.out.println(combo);      
   String[] nameParts = combo.split("[\\[\\],]");
   System.out.println(nameParts[0]);
  }
  }
This is not working
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 18785429
You need to do

System.out.println(nameParts[1]);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18785451
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 136
eclipse buid path vs tomcat lib path 10 49
Better way to make a string with template variables in java 3 53
printf performancy 11 105
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question