#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0

    check for extra spaces


    how do i check if a string contains extra spaces. eg:"try erte"
    in the above string, it contains more than 1 space, how do i remove the extra spaces??

    codes would be helpful.
    thanks... pls help asap....
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    peter, can you upgrade to jdk 1.4? All this text searching stuff that you're doing would be dirt simple with the regex functions.
  4. #3
  5. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Turns out this was really easy.

    Code:
    <%@page contentType="text/html"
    %><%@page import="java.util.StringTokenizer"
    %><html>
    <head><title>JSP Page</title></head>
    <body>
    <%
    String testString = "   This is      a   test    string to    print    out        ";
    String newString = "";
    StringTokenizer st = new StringTokenizer( testString);
    while (st.hasMoreTokens()) {
    	 newString += st.nextToken();
    	 newString += " ";
    }
    newString = newString.trim();
    %>
    <p>The new string is:</p>
    <p><%= newString %></p>
    </body>
    </html>

IMN logo majestic logo threadwatch logo seochat tools logo