In Objective-C for iOS, how would I remove the last character of a string using a button action?
4 Answers
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.

- 17,282
- 18
- 107
- 195

- 40,399
- 3
- 75
- 82
-
8Your solution has an off by 1 bug. It should be [string substringToIndex:[string length] - 1]; – Jim Correia Jul 04 '09 at 14:46
-
Does "string" refer to "string.text" or just the string name – Omar Jul 05 '09 at 08:44
-
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. – Marc Charbonneau Jul 06 '09 at 12:39
-
1@DonalRafferty make sure your string is not nil – quantumpotato Jan 05 '12 at 21:07
-
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. – Marc Charbonneau Jan 06 '12 at 18:00
-
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. – user1071136 Aug 12 '12 at 16:05
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)];

- 58
- 5

- 242,470
- 58
- 448
- 498
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.

- 1
- 1

- 15,636
- 4
- 42
- 61
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];
...

- 9,676
- 29
- 53
-
1NSMakeRange can sometimes make filling out a range struct look a little more elegant. – dreamlax Jul 04 '09 at 13:40
-
@Harald, `substringWithRange:` returns a `NSString*`. Take note of the pointer. – Thomas Nadin Jan 06 '12 at 10:56