Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

How to dedect 4 consecutive space

Thread view: 
elingtse@gmail.com - 31 Dec 2005 10:48 GMT
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
Bart Cremers - 31 Dec 2005 10:53 GMT
String s = "         XXXXX      XXXXXX";
String ts = s.replaceAll(" {4}", "\t");

Regards,

Bart
elingtse@gmail.com - 31 Dec 2005 11:13 GMT
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 ?
Bart Cremers - 31 Dec 2005 23:05 GMT
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
elingtse@gmail.com - 01 Jan 2006 04:28 GMT
> String s = new String(charArray);
> charArray = s.replaceAll(" {4}", "\t").toCharArray();
[quoted text clipped - 6 lines]
>
> Bart

It works perfectly.

Thanks Bart & Larry, you do me a great favor.
Larry Barowski - 31 Dec 2005 12:01 GMT
> HI all,
>
[quoted text clipped - 16 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.