Solved

How to replace two different delimiters in a string with some characters in Java?

Posted on 2013-11-13
11
356 Views
Last Modified: 2013-11-14
Hi,
I have the following string and I would like to replace all the spaces and commas in this string with some characters.

Please see the example below:

Input string:

12345, 234567 3456789

Open in new window


Expected output string:

-c 12345 -xyz\r\n-c 234567 -xyz\r\n-c 3456789 -xyz

Open in new window


how can I do it in Java?
0
Comment
Question by:Tolgar
  • 6
  • 5
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39644772
I would do it in two stages

a. tokenize the string
b. place the tokens in a new one

a. has already been answered elsewhere

b. (assuming that's a command)

String command = String.format("-c %s -xyz\\r\\n-c %s -xyz\\r\\n-c %s -xyz", tokens[0], tokens[1], tokens[2]);

Open in new window

0
 

Author Comment

by:Tolgar
ID: 39644800
Sorry, I couldn't understand it. Can you please send me code with the tokenizing part?

And example that I sent you has three numbers in it but there can be any number of numbers in the input string.

Example2:

12345, 234567 3456789, 345672 4532344566,4366, 46665467

Open in new window


Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39644901
Sorry, I couldn't understand it. Can you please send me code with the tokenizing part?
That's already been answered by kaufmed

And example that I sent you has three numbers in it but there can be any number of numbers in the input string.
And the same prefix goes before every number?
0
 

Author Comment

by:Tolgar
ID: 39644936
@CEHJ: The code you sent me is only for 3 numbers. If you can send me the whole code (inlcuding kaufmed's answer). that would help me better.

Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39644961
I'm sorry, but i'm not going to provide you with a record of your own answered questions
You also didn't answer the question i just asked you ...
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:Tolgar
ID: 39645134
oh, sorry, I didn't read your last line as a question.

And yes, the same prefix goes before every number.
0
 

Author Comment

by:Tolgar
ID: 39645204
ok. This is what I have with  Kaufmed's code in it:
            String ListNumbers = listNum.replaceAll("[ ,]+", "\r\n-c ");
            populatechangelist("-c " + ListNumbers + "\r\n");

Open in new window


The code above only add "-c " in the beginning of each number by replacing space or comma in between the numbers.

However, I also need to add "-xyz" after any number. And I cannot do it.

Do you have any idea?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39645600
This "\r\n" - are you intending that to be a linefeed?
0
 

Author Comment

by:Tolgar
ID: 39645836
Yes, that's right.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39646042
Try

    public static String getParameters(String s) {
        final String LINEFEED = System.getProperty("line.separator");
        StringBuilder result = new StringBuilder();
        String[] atoms = s.split("[ ,]+");
        String sep = "";

        for (String atom : atoms) {
            result.append(String.format("%s-c %s -xyz", sep, atom));
            sep = LINEFEED;
        }

        return result.toString();
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39648726
:)
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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

943 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

10 Experts available now in Live!

Get 1:1 Help Now