How to dedect 4 consecutive space

Discussion in 'Java' started by elingtse@gmail.com, Dec 31, 2005.

  1. Guest

    HI all,

    I need to replace a tab for every 4 consecutive spaces for a statement
    which is included in an array. I've set another index array to mark
    whether the content is space, if the index become 4, if change to tab.
    But it seems hard to go back the previous index to delete the previous
    spaces in a for loop. Could anyone give me some hints ? Thanks a lot.

    Statement Example :

    s = space
    t = tab
    X = content

    sssssssssXXXXXssssssXXXXXX

    Expected Solution :

    ttsXXXXXtssXXXXXX
    (replace every consecutive 4 space into tab)

    et
     
    , Dec 31, 2005
    #1
    1. Advertising

  2. Bart Cremers Guest

    String s = " XXXXX XXXXXX";
    String ts = s.replaceAll(" {4}", "\t");

    Regards,

    Bart
     
    Bart Cremers, Dec 31, 2005
    #2
    1. Advertising

  3. Guest

    Thanks Bart, but it did not work because I am using a char array to
    hold the statement, String method cannot be used here. Any other
    suggestion ?
     
    , Dec 31, 2005
    #3
  4. <> wrote in message
    news:...
    > HI all,
    >
    > I need to replace a tab for every 4 consecutive spaces for a statement
    > which is included in an array. I've set another index array to mark
    > whether the content is space, if the index become 4, if change to tab.
    > But it seems hard to go back the previous index to delete the previous
    > spaces in a for loop. Could anyone give me some hints ? Thanks a lot.
    >
    > Statement Example :
    >
    > s = space
    > t = tab
    > X = content
    >
    > sssssssssXXXXXssssssXXXXXX
    >
    > Expected Solution :
    >
    > ttsXXXXXtssXXXXXX
    > (replace every consecutive 4 space into tab)


    But you probably need to do more than that for a real "tabify".
    Consider:

    stXXX, sstXXX, or ssstXXX all become tXXX
    and probably sXXX becomes tXXX
    XsssX becomes XtX, XXssX becomes XXtX
    and probably XXXsX becomes XXXtX

    For a tabify operation, your above example is incorrect.
    To get the same layout,
    sssssssssXXXXXssssssXXXXXX becomes
    ttsXXXXXttXXXXXX

    Maintain a screen position separately from the character
    position, and update it differently when a tab is
    encountered:
    screen_pos += TAB_SIZE - (screen_pos % TAB_SIZE).
    Keep a running count of consecutive spaces. If a series of
    spaces ends at (screen_pos % TAB_SIZE) == TAB_SIZE -1,
    replace them with a tab. Else if a series of spaces ends at a tab,
    delete them. For a character array, you can do this in place
    by keeping a "follower" index and copying characters to
    there.
     
    Larry Barowski, Dec 31, 2005
    #4
  5. Bart Cremers Guest

    String s = new String(charArray);
    charArray = s.replaceAll(" {4}", "\t").toCharArray();

    This should work for the simple case of replacing 4 consecutive spaces
    with a single tab.

    As Larry pointed out, it would be more complex if you need to consider
    correct tab stops and stuff.

    Bart
     
    Bart Cremers, Dec 31, 2005
    #5
  6. Guest

    Bart Cremers wrote:
    > String s = new String(charArray);
    > charArray = s.replaceAll(" {4}", "\t").toCharArray();
    >
    > This should work for the simple case of replacing 4 consecutive spaces
    > with a single tab.
    >
    > As Larry pointed out, it would be more complex if you need to consider
    > correct tab stops and stuff.
    >
    > Bart


    It works perfectly.

    Thanks Bart & Larry, you do me a great favor.
     
    , Jan 1, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jas Shultz
    Replies:
    0
    Views:
    993
    Jas Shultz
    Dec 3, 2003
  2. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,980
    Bryan Bullard
    Jul 11, 2003
  3. Christian Seberino
    Replies:
    21
    Views:
    1,801
    Stephen Horne
    Oct 27, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    1,107
    Steve Lamb
    Oct 23, 2003
  5. Ian Bicking
    Replies:
    2
    Views:
    784
    Michael Hudson
    Oct 24, 2003
Loading...

Share This Page