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. Commented Jan 25, 2012 at 15:32
  • String.replace() ...examples to follow no doubt ;-) Commented Jan 25, 2012 at 15:32

7 Answers 7

12

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");
}
Sign up to request clarification or add additional context in comments.

1 Comment

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").
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 Comments

Look to my answer for that, it's part of the JDK.
Rush to answer a simple question and make simple mistakes :)
Also, see my comment on maerics' answer re: replace and replaceall.
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 Comment

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

Comments

1

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

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.