Today i play around with some funny and simple thing in Java, frankly i really do not know about it. I created following code like below
if(StringUtils.hasLength(sText) { sText= sText.replaceAll(" ", " AND "); }
What i trying to do is remove all whitespace between a string. for example, user key in “Hello Google”, it will replace as “Hello AND Google”, ya this is correct if user only enter one space between word.
"Hello Google" ---> "Hello AND Google"
But how about user key in many space in between? Haha.. For example
"Hello Google" ---> "Hello AND AND AND AND AND Google"
Above is not what i want, should i replace it as below?
if(StringUtils.hasLength(sText) { sText= sText.replaceAll(" ", " AND "); sText= sText.replaceAll(" ", " AND "); sText= sText.replaceAll(" ", " AND "); sText= sText.replaceAll(" ", " AND "); sText= sText.replaceAll(" ", " AND "); }
ya we can replace as many as we like, but this is not so effective and dynamic enough. Finally Regular Expression come to place and solve it.
if(StringUtils.hasLength(sText) { sText= sText.replaceAll("\\s+", " AND "); }
Great , exactly what i want, Regular Expression ROCK!!!

















Thank you!!
Exactly what I was looking for!
Cheers
Regular Expression IS ROCK ~