2

I have an UITextView which is for instance 380 characters in length:

NSLog(@"aTextView.text lenght %i", aTextView.text.length);

I now want to go through this text (backwards, char by char) and delete all characters which come before the last space (e.g. if the last words were "...this is an example", it want to reduce the string to "...this is an ":

         for (int i = aTextView.text.length-1; i > 0; i--) {

         NSString *checkedChar = [NSString stringWithFormat:@"%c", [aTextView.text characterAtIndex:i]];
         NSLog(@"I currently check: %@", checkedChar);

         if ([checkedChar isEqualToString:@" "]) {
             // job done
             i = 0; // this ends the loop
         } else {

I need something like [aTextView.text removeCharacterAtIndex:i];

         }

     }

How do I achieve this? I couldn't find any methods in the docs and would be very grateful for suggestions.


EDIT:

 NSString *myString = aTextView.text;

         NSRange range = [myString rangeOfString:@" " options:NSBackwardsSearch];
         NSString *oldText = [myString subStringToIndex:range.location];
         NSString *newText = [myString subStringFromIndex:range.location];

         NSLog(@"++++ OLD TEXT ++++: %@", oldText);
         NSLog(@"++++ NEW TEXT ++++: %@", newText);

         aTextView.text = oldText;

This crashes my app... I am calling this from - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText

I get the error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString subStringToIndex:]: unrecognized selector sent to instance 0x4e33850'

And xCode gives me the warning the subStringToIndex may not respond...

n.evermind
  • 11,944
  • 19
  • 78
  • 122

2 Answers2

4

You don't need a loop to do this - take a look at NSString' rangeOfString:options: and substringToIndex: methods. For example :

NSRange range = [myString rangeOfString:@" " options:NSBackwardsSearch];

NSString *newString = [myString substringToIndex:range.location];

Hope that helps.

NB Don't forget to check that your string definitely contains a space ;)

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Thanks, that looks so beautifully simple. Just one additional question: How can I get whatever was cut off my string? I.e. If I have "... this is an example" and get "...this is an " into newString, how can I get "example" into deletedString? – n.evermind May 20 '11 at 13:41
  • 1
    `[myString substringFromIndex:range.location]` gets the other half ;) – deanWombourne May 20 '11 at 13:52
  • Thanks! Also, I just tried this, but it crashed my app...: -[NSCFString subStringToIndex:]: unrecognized selector sent to instance 0x4b69c30 ? xCode give me the warning: NSString may not respond to substringto and fromindex... I updated my code, so you can see what I did – n.evermind May 20 '11 at 13:55
  • I guess the problem is that I'm trying to do it with aTextView.text? I'm sure it doesn't like that. Do I need to convert aTextView.text to an array before I start messing around? – n.evermind May 20 '11 at 14:00
  • 1
    lowercase 's' in the word string - not 'subStringToIndex', 'substringToIndex' :) – deanWombourne May 20 '11 at 14:09
  • 1
    The text property of a UITextView is just an NSString so it will work absolutely fine - no need for 'NSArray's at all. Take a look at the documentation - it's all for NSString, not NSArray! – deanWombourne May 20 '11 at 14:12
  • Thanks... I guess I should have spotted that myself. There's still a lot I need to learn. I didn't show up in blue when I typed it... and obviously, when I looked it up, i didn't realise it had small s. THANKS SO MUCH FOR YOUR HELP! – n.evermind May 20 '11 at 14:17
  • Sorry to bother again, but how do I actually check if my string does contain a space? I just entered a lot of returns and, as was to be expected, I crashed the app: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFString substringToIndex:]: Range or index out of bounds' *** Call stack at first throw: – n.evermind May 20 '11 at 14:28
  • Just solved it with if (range.location == NSNotFound) { return NO; } – n.evermind May 20 '11 at 14:44
  • That's exactly the way to solve it :) (And I always type subString each time I use those methods!) – deanWombourne May 20 '11 at 16:55
0

This is very easy in iOS 5 or later:

// This is your delete button method.
-(IBAction)btnDelete:(id)sender
{
    // txtView is UITextView
    if([txtView.text length] > 0)
    {
        [txtView deleteBackward];
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
TheTiger
  • 13,264
  • 3
  • 57
  • 82