Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to split a string by comma or any number of space in Java and remove some pattern?

Posted on 2011-10-14
4
Medium Priority
?
186 Views
Last Modified: 2012-05-12
Hi,
I have this code in JAVA:

java.lang.String[] something = args[1].split(",");	

Open in new window


It splits the string by using commas in the original string and assigns it into an array.

Now what I wanna do is:

I want to split by comma or any number of space in between and remove all "g's" as a prefix:

Let's say I have this:

111111, 222222      333333,g444444     g555555

I wanna split this string and I wanna get this in the array:

111111
222222
333333
444444
555555


How can I do it by changing this code slightly in one or two lines lines?

Thanks,
0
Comment
Question by:Tolgar
  • 3
4 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36969518
split([,g\\s]+);
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36969569
This is working:
           String IDListLink =  "111111,g222222  333333";

        String [] ss25 = IDListLink.split("[,g\\s]+");
        for(String s25:ss25){
            System.out.println(s25);
            

        }

Open in new window



111111
222222
333333

Open in new window

0
 

Author Comment

by:Tolgar
ID: 36969819
@for_yan: That's great!!! Can you please briefly tell me how you read this command:

split([,g\\s]+);

Open in new window


What does each term represent?

Thanks,

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36969874
this splits on any number of consecutive commas, spaces and g characters
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

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…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

571 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