1

I have a string The quick * fox jumps * the * dog and I have an array of strings String[] array = {"brown", "over", "lazy"}.

What is the optimal way to replace all * to strings from array then first * must be replaced to array[0] element, the second * to array[1] etc. Of course solution must allow N replacement for N elements in array.

4
  • The * is mandatory? (as opposed to using %s) Commented Jun 10, 2017 at 13:19
  • @Marvin Yes, I need * Commented Jun 10, 2017 at 13:20
  • What should happen if there are less array entries than *? Commented Jun 10, 2017 at 13:21
  • @Marvin In my code that is not possible. Commented Jun 10, 2017 at 13:22

6 Answers 6

3
String.format("The quick * fox jumps * the * dog".replace("*", "%s"), array);
> The quick brown fox jumps over the lazy dog

replace * to %s and use String.format with the parameters, this way works.

see more: How to format strings in Java

Sign up to request clarification or add additional context in comments.

3 Comments

Up-vote for being the simplest solution, one valid interpretation of "optimal way".
replaceAll uses regex syntax where * has special meaning. We don't need regex here, replace method would be better since it doesn't use regex syntax (it automatically escapes it) and also replaces all occurrences of searched string.
Thanks @Pshemo :)
2

Use appendReplacement functionality of Java regex library:

StringBuffer res = new StringBuffer();
Pattern regex = Pattern.compile("[*]");
Matcher matcher = regex.matcher("Quick * fox jumps * the * dog");
int pos = 0;
String[] array = {"brown", "over", "lazy"};
while (matcher.find()) {
    String replacement = pos != array.length ? array[pos++] : "*";
    matcher.appendReplacement(res, replacement);
} 
matcher.appendTail(res);

Demo.

2 Comments

If pos == array.length, then just exit the loop, and appendTail() will append the rest. Faster than replacing * with *. --- Note, it is even allowed to skip a call to appendReplacement() during the iteration, e.g. if you skip every even *, then result will only replace the odd * markers.
@Andreas I'm sure OP will figure out the details of what happens when there are additional placeholders. I just wanted to show him code that does not crash.
2
for (String x : array) {
    yourString = yourString.replaceFirst("\\*", x);
}

1 Comment

replaceFirst is using regex syntax where * is metacharacter. To make it literal we need to escape it.
0

In all cases you need to reconstruct the String, and the best way to do so is to use StringBuilder

Two ways to do so :

First

  • Iterate by characters over the string and add it to the StringBuilder if it's not *
  • If the character is *, add the string at index variable that represents the pointer to the strings array
  • Increase index by 1.

Second

  • Split the original String over * using split() method, this gives you an array of strings of the original String without the *
  • Iterate once over the two arrays at the same time, add the item of the array of splits and then add the corresponding index at the second replacement array.

Comments

0

If by "optimal way" you mean performance, then use a loop with indexOf() and build the result using a StringBuilder.

Other answers have already covered "simple" (i.e. less code), if that's what you meant by "optimal way".

String input = "The quick * fox jumps * the * dog";
String[] array = {"brown", "over", "lazy"};

StringBuilder buf = new StringBuilder();
int start = 0;
for (int i = 0, idx; i < array.length; i++, start = idx + 1) {
    if ((idx = input.indexOf('*', start)) < 0)
        break;
    buf.append(input.substring(start, idx)).append(array[i]);
}
String output = buf.append(input.substring(start)).toString();

System.out.println(output);

Output

The quick brown fox jumps over the lazy dog

This code will silently accept too many or too few values in the array.

Comments

0

Write a for-loop iterating over each character in the String The quick * fox jumps * the * dog and every time you encounter *, you pick the next element of the array while maintaining the current index.

String text = "The quick * fox jumps * the * dog";
String[] elements = {"brown", "over", "lazy"};

int idx = 0;
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
    if (c == '*')
        result.append(elements[idx++]);
    else
        result.append(String.valueOf(c));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.