Solved

Spliting a string word into segments

Posted on 2007-03-24
6
187 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now