Replace certain string in array of strings
Asked Answered
C

7

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?

Coincident answered 25/1, 2012 at 15:28 Comment(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.Keri
String.replace() ...examples to follow no doubt ;-)Flittermouse
K
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");
}
Keri answered 25/1, 2012 at 15:31 Comment(1)
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").Boxhaul
R
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.

Rife answered 25/1, 2012 at 15:31 Comment(3)
Look to my answer for that, it's part of the JDK.Keri
Rush to answer a simple question and make simple mistakes :)Rife
Also, see my comment on maerics' answer re: replace and replaceall.Keri
B
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");
}
Boxhaul answered 25/1, 2012 at 15:32 Comment(1)
Note that String#replace(target, replacement) should do the trick. replace does the same as replaceAll, but doesn't work with RegExes.Keri
S
2
String[] test={"hahaha lol","jeng jeng jeng","stack overflow"};
                for (int i=0;i<test.length;i++) {
                    test[i]=test[i].replaceAll(" ", "%20");
                }
Staciastacie answered 25/1, 2012 at 15:33 Comment(0)
W
1

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

Weizmann answered 25/1, 2012 at 15:31 Comment(0)
M
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.

Maura answered 25/1, 2012 at 15:33 Comment(0)
M
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

Molest answered 20/12, 2020 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.