Solved

Spliting a string word into segments

Posted on 2007-03-24
6
186 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
Comment Utility
String[] nameParts = name.split("[\\[\\,]]");
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Typo sorry


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

Expert Comment

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

Regards,
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:mr_usmannaeem
Comment Utility
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
Comment Utility
You need to do

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

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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