• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

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

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
Tolgar
Asked:
Tolgar
  • 6
  • 5
1 Solution
 
CEHJCommented:
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
 
TolgarAuthor Commented:
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
 
CEHJCommented:
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!

 
TolgarAuthor Commented:
@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
 
CEHJCommented:
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
 
TolgarAuthor Commented:
oh, sorry, I didn't read your last line as a question.

And yes, the same prefix goes before every number.
0
 
TolgarAuthor Commented:
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
 
CEHJCommented:
This "\r\n" - are you intending that to be a linefeed?
0
 
TolgarAuthor Commented:
Yes, that's right.
0
 
CEHJCommented:
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
 
CEHJCommented:
:)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now