PDA

View Full Version : check a string for extra spaces


hongyi
09-24-2002, 02:10 AM
How do i use JSP to check if a string contains extra spaces.

Eg: Hel lo. the user accidentally added a extra space in the string Hello. how do i remove it??

whammy
09-28-2002, 06:01 PM
Not sure if this will work, but from what I've seen JSP is pretty similar to ASP... in ASP I'd do:


Function RemoveSpaces(str) ''''''''''''''''''''''
If IsNull(str) Then str = ""
Dim rsRegEx
Set rsRegEx = New RegExp
rsRegEx.Pattern = "\s"
rsRegEx.Global = True
RemoveSpaces = rsRegEx.Replace(str,"")
End Function ''''''''''''''''''''''''''''''''''''

myString = RemoveSpaces(myString)


And in javascript it would be:


myString = myString.replace(/\s/g,'')


Going by this (http://www.webreview.com/2001/12_10/developers/index01.shtml) page, I'd say it would be something like:

Pattern p = Pattern.compile(" ");
String toMatch = "he llo";
Matcher m = p.matcher(toMatch);
String replaced = m.replaceAll("");

but don't quote me on that as I have never used JSP and don't have any method whatsoever to test it!