Results 1 to 4 of 4
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Checking if string contains only tabs/spaces/newlines/carriage returns

    I've been struggling with this one, supposedly simple, method for over an hour now, and I just dont get it. I'm trying to check if a String contains no visible content... e.g. it is like "\t \n \t\r", but nothing I do works. Could anyone give me a regex or something to call matches(regex) with? I cant seem to find one, and I've tried a for loop through the char array from getCharArray(), etc.

    Thanks in advance,
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I'm trying to check if a String contains no visible content
    so are you trying to check for whitespace or non-printable character?
    Simple way is to use a loop with string.charAt() and check its octal / hex value.

    somehting like:
    Java Code:
    static boolean isNonPrintable(String s){
    for(int i=0; i<s.length(); ++i){
      char c = s.charAt(i);
      if(c<'/033' || c>'/0127') // fill in the ascii oct values
        return true;
    }
    return false
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Java Code:
    string.matches("\\s*");
    Replace * with + if you want to match only non-empty Strings.

    db

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    My bad...

    That part was fine, I just didn't realize a NodeList was backed by the actual Node content. (XML Parsing)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. How to display a String that contains more than one white spaces
    By thachun in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-27-2009, 10:16 AM
  2. What's the need for co-variant returns ?
    By ajeeb in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 10:56 AM
  3. How do i add spaces ~(very simple)
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 11-02-2008, 03:01 AM
  4. Replies: 15
    Last Post: 09-29-2008, 08:18 AM
  5. New line or Carriage Return through FileWriter
    By johnt in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 10:13 PM

Posting Permissions

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