Replace certain string in array of strings

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");
}

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.


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");
}