i would like to remove the last line from my string.. the nsstring object here is _text. My thoughts are to scan characters from the end and if i found a carriage return symbol i substring to that index, and finnish. But i don’t want to remove every carriage return, just the last one.
So i would like to do something like this:
for (int i = [_text length] ; i>0; i--) {
char character = [_text characterAtIndex:i];
if (character == @"\n") {
_text = [_text substringToIndex:i];
return;
}
}
Any help would be very appreciated!
Thanks.
You should handle the last char being whitespace or CR.
You also had a bug where you needed length – 1 in the for loop.
Here’s some working code:
This outputs:
2011-09-22 08:00:10.473 Craplet[667:707] line number 1
line number 2