Solved

Spliting a string word into segments

Posted on 2007-03-24
6
188 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
factorial example 4 39
servlet filter example 37 61
expectj telnet failing 5 36
Convert from a json string array to a Java object 3 31
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

770 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