?
Solved

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

Posted on 2013-11-13
11
Medium Priority
?
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 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