Solved

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

Posted on 2013-11-13
11
353 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 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

18 Experts available now in Live!

Get 1:1 Help Now