Spliting a string word into segments

Posted on 2007-03-24
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.

Question by:mr_usmannaeem
  • 4
LVL 86

Expert Comment

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

Expert Comment

ID: 18785148
Typo sorry

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

Expert Comment

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

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

Accepted Solution

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

LVL 86

Expert Comment

ID: 18785451

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 52
difference between sorce folder and folder in eclipise 3 29
SequenceInputStream example 3 19
swing controls 2 16
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

830 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