+ 2
Why I obtain Hello123 as a result? And what is the aim of the first argument "a-z"?
System.out.print("Hello123",replace all("a-z",""));
9 Answers
+ 2
This is how you should do it-
System.out.print("Hello123".replaceAll("[a-z]",""));
It is called Regex(Regular expression) and it is used to search or manipulate a string pattern. Here [a-z] is the range of characters which if present in the string are replaced with "".
+ 3
System.out.print("Hello123".replaceAll("[a-z]","");
Here [a-z] is a regex used to find a to z charecters. If found, then it replaced by "".
But in question, it is given as a-z only. So if the string "Hello123" contains exactly, then it replaced with "". But the string does not contain the pattern like a-z. So it simply return same.......
Try this to see for differece:
System.out.print("Helloa-z123".replaceAll("a-z",""));
System.out.print("Hello123".replaceAll("a-z",""));
+ 1
It will give you an error if you did so and it will not print anything. If you look closely you can see that replaceAll() method that you have declared is not right.
+ 1
Hichem GOUIA
Wel come...
+ 1
Jayakrishna so it searches for string "a-z" in string "Hello123" not for any lower case letter in [a-z]
+ 1
Josée Mallah
Yes.
If you need search for any lower case letter, then correct statement is
System.out.print("Hello123".replaceAll("[a-z]","");
0
So clear thanks
But if we put "a-z" instead of "[a-z]" like the example that I found, it replaces nothing!! Why?
0
In a challenge question I find it like this System.out.print("hello123", replaceAll("a-z",""));
Without [ ]
So maybe the question was wrong ...
0
Jayakrishna thanks for your explanation đ