Objective-C - Remove last character from string

111,117

Solution 1

In your controller class, create an action method you will hook the button up to in Interface Builder. Inside that method you can trim your string like this:


if ([string length] > 0) {
    string = [string substringToIndex:[string length] - 1];
} else {
    //no characters to delete... attempting to do so will result in a crash
}






If you want a fancy way of doing this in just one line of code you could write it as:

string = [string substringToIndex:string.length-(string.length>0)];

*Explanation of fancy one-line code snippet:

If there is a character to delete (i.e. the length of the string is greater than 0)
     (string.length>0) returns 1 thus making the code return:
          string = [string substringToIndex:string.length-1];

If there is NOT a character to delete (i.e. the length of the string is NOT greater than 0)
     (string.length>0) returns 0 thus making the code return:
          string = [string substringToIndex:string.length-0];
     Which prevents crashes.

Solution 2

If it's an NSMutableString (which I would recommend since you're changing it dynamically), you can use:

[myString deleteCharactersInRange:NSMakeRange([myRequestString length]-1, 1)];

Solution 3

The solutions given here actually do not take into account multi-byte Unicode characters ("composed characters"), and could result in invalid Unicode strings.

In fact, the iOS header file which contains the declaration of substringToIndex contains the following comment:

Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters

See how to use rangeOfComposedCharacterSequenceAtIndex: to delete the last character correctly.

Solution 4

The documentation is your friend, NSString supports a call substringWithRange that can shorten the string that you have an return the shortened String. You cannot modify an instance of NSString it is immutable. If you have an NSMutableString is has a method called deleteCharactersInRange that can modify the string in place

...
NSRange r;
r.location = 0;
r.size = [mutable length]-1;
NSString* shorted = [stringValue substringWithRange:r];
...
Share:
111,117

Related videos on Youtube

Skryge
Author by

Skryge

Updated on June 27, 2020

Comments

  • Skryge
    Skryge almost 4 years

    In Objective-C for iOS, how would I remove the last character of a string using a button action?

  • TOMKA
    TOMKA almost 15 years
    NSMakeRange can sometimes make filling out a range struct look a little more elegant.
  • user3327101
    user3327101 almost 15 years
    Your solution has an off by 1 bug. It should be [string substringToIndex:[string length] - 1];
  • Skryge
    Skryge almost 15 years
    Does "string" refer to "string.text" or just the string name
  • Werner
    Werner almost 15 years
    Thanks Jim, it was too early in the morning for arithmetic I guess. :) Omar, string refers to a variable called string that holds the text you want to modify. You can take a look at the documentation for the different ways you can create an NSString object, either from a file on disk or from data in your application.
  • quantumpotato
    quantumpotato over 12 years
    @DonalRafferty make sure your string is not nil
  • Thomas Nadin
    Thomas Nadin over 12 years
    @Harald, substringWithRange: returns a NSString*. Take note of the pointer.
  • Werner
    Werner over 12 years
    It's okay if string is nil-- calling a method which returns an integer value will return 0 in Obj-C (note that the same is NOT true for floating point return values). So that if statement is equivalent to asking string != nil && [string length] > 0. Chances are Donal's string variable is being over released.
  • user1071136
    user1071136 over 11 years
    This solution does not take into account composed characters (Unicode letters which take more than one character to encode), and could result in an invalid Unicode string.