Drake
Drake

Reputation: 93

What does this java line do?

response1 = CustomHttpClient.executeHttpPost("http://gamesdsxd.com/appfiles/login.php", postParameters);
String res = response1.toString();
res = res.replaceAll("\\s+", "");

I'm wondering what \s+ does and what this replaceAll does and why it is needed.

Upvotes: 1

Views: 505

Answers (4)

Lalit Poptani
Lalit Poptani

Reputation: 67286

Here is what replaceAll will do with a small example:

String str = "This5is5testing5of5replaceAll";
        str = str.replaceAll("5", " ");
        System.out.println(str);

Here the output will be: This is testing of replaceAll

5 will be replaced by space everywhere.

Upvotes: 2

James Lopez
James Lopez

Reputation: 21

it replaces double spaces or more to no space. For example: " " will transform into ""; " " will transform into ""; but " " will still be equal to " ".

Upvotes: 0

Adnan Bhatti
Adnan Bhatti

Reputation: 3480

\s+ is a regular expression. It matches strings which consist of at least one white space character. \s is actually a meta character. Here it is removing white space.

Here is the replaceAll documentation.

replaceAll(String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement

Upvotes: 1

Dan W
Dan W

Reputation: 5782

The \s+ replaces all occurrences of a space " " with no space "".

For more help, see this link: Removing Whitespace Between String.

Upvotes: 4

Related Questions