Solved

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

Posted on 2013-11-13
11
358 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

832 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