Re: Remove excess line breaks
Re: Remove excess line breaks
- Subject: Re: Remove excess line breaks
- From: p3consulting <email@hidden>
- Date: Mon, 15 Dec 2003 17:29:47 +0100
On 15 dic. 2003, at 13:22, Jens Bauer wrote:
Hi Rick,
Try looking at the NSString snippet on <URL:http://objc-source.com/>
On Monday, Dec 15, 2003, at 12:53 Europe/Copenhagen, Rick Anderson
wrote:
I'm trying to remove excess line breaks from a string, replacing any
given number of returns with one single return. If I were doing it in
perl, it would look like this:
$theString =~ s/\r+/\r/g
So, here's what I come up with in Cocoa. If it looks a little
convoluted, well... it is. And it also doesn't work. Any thoughts
with what's wrong with this or possibly a more elegant way to do it
would be appreciated. The first line is just a test to see if the
string still contains any occurrence of a double return; the third
line simply runs through and replaces any double returns with a
single return.
if ( (NSEqualRanges ([endString rangeOfString:@"\r\r"],
NSMakeRange(NSNotFound, 0) ) ) == NO)
{
[endString replaceOccurrencesOfString:@"\r\r" withString:@"\r"
options:nil range:NSMakeRange(0, [endString length])];
}
Should be a "while" not a "if" to make the Cocoa version perform
exactly as the Perl one.
After replacing an occurence, the replace algorithm continues after the
replacement, so 3 CRs becomes 2 CRs not 1 CR as expected.
(4 CRs -> 2 CRs, 5 CRS -> 3 CRs etc.)
Pascal Pochet
P3 Consulting
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.