7

let's say I have this string array in java

String[] test = {"hahaha lol", "jeng jeng jeng", "stack overflow"};

but now I want to replace all the whitespaces in the strings inside the array above to %20, to make it like this

String[] test = {"hahaha%20lol", "jeng%20jeng%20jeng", "stack%20overflow"};

How do I do it?

2
  • Note that your question and its title diverge - your question is very specific, while the title is very general. My answer below goes into the question, not the title.
    – Urs Reupke
    Jan 25, 2012 at 15:32
  • String.replace() ...examples to follow no doubt ;-)
    – AJG85
    Jan 25, 2012 at 15:32

7 Answers 7

11

Iterate over the Array and replace each entry with its encoded version.

Like so, assuming that you are actually looking for URL-compatible Strings only:

for (int index =0; index < test.length; index++){
  test[index] = URLEncoder.encode(test[index], "UTF-8");
}

To conform to current Java, you have to specify the encoding - however, it should always be UTF-8.

If you want a more generic version, do what everyone else suggests:

for (int index =0; index < test.length; index++){
    test[index] = test[index].replace(" ", "%20");
}
1
  • 2
    Note that, annoyingly, URLEncoder.encode(str) is deprecated in favor of its overloaded form which takes the encoding type as a second argument (which should always be "UTF-8").
    – maerics
    Jan 25, 2012 at 20:25
4

Here's a simple solution:

for (int i=0; i < test.length; i++) {
    test[i] = test[i].replaceAll(" ", "%20");
}

However, it looks like you're trying to escape these strings for use in a URL, in which case I suggest you look for a library which does it for you.

3
  • 1
    Look to my answer for that, it's part of the JDK.
    – Urs Reupke
    Jan 25, 2012 at 15:36
  • Rush to answer a simple question and make simple mistakes :)
    – user130076
    Jan 25, 2012 at 15:46
  • Also, see my comment on maerics' answer re: replace and replaceall.
    – Urs Reupke
    Jan 26, 2012 at 15:17
3

Try using String#relaceAll(regex,replacement); untested, but this should work:

for (int i=0; i<test.length; i++) {
  test[i] = test[i].replaceAll(" ", "%20");
}
1
  • Note that String#replace(target, replacement) should do the trick. replace does the same as replaceAll, but doesn't work with RegExes.
    – Urs Reupke
    Jan 26, 2012 at 15:15
2
String[] test={"hahaha lol","jeng jeng jeng","stack overflow"};
                for (int i=0;i<test.length;i++) {
                    test[i]=test[i].replaceAll(" ", "%20");
                }
1

for each String you would do a replaceAll("\\s", "%20")

0

Straight out of the Java docs... String java docs

You can do String.replace('toreplace','replacement').

Iterate through each member of the array with a for loop.

0

You can use IntStream instead. Code might look something like this:

String[] test = {"hahaha lol", "jeng jeng jeng", "stack overflow"};

IntStream.range(0, test.length).forEach(i ->
        // replace non-empty sequences
        // of whitespace characters
        test[i] = test[i].replaceAll("\\s+", "%20"));

System.out.println(Arrays.toString(test));
// [hahaha%20lol, jeng%20jeng%20jeng, stack%20overflow]

See also: How to replace a whole string with another in an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.