let\'s say I have this string array in java
String[] test = {"hahaha lol", "jeng jeng jeng", &quo
for each String you would do a replaceAll("\\s", "%20")
String[] test={"hahaha lol","jeng jeng jeng","stack overflow"};
for (int i=0;i<test.length;i++) {
test[i]=test[i].replaceAll(" ", "%20");
}
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.
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
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.
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");
}