Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2002
    Thanked 0 Times in 0 Posts

    check a string for extra spaces

    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??

  • #2
    Senior Coder
    Join Date
    Jun 2002
    41° 8' 52" N -95° 53' 31" W
    Thanked 0 Times in 0 Posts
    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 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!
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts