Objective-C - Remove last character from string - Printable Version +- 0Day Forums (https://0day.red) +-- Forum: Coding (https://0day.red/Forum-Coding) +--- Forum: Objective-C (https://0day.red/Forum-Objective-C) +--- Thread: Objective-C - Remove last character from string (/Thread-Objective-C-Remove-last-character-from-string) |
Objective-C - Remove last character from string - overdared375371 - 07-21-2023 In Objective-C for iOS, how would I remove the last character of a string using a button action? RE: Objective-C - Remove last character from string - Sirunquieter15 - 07-21-2023 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]; ... RE: Objective-C - Remove last character from string - leanngnvjlg - 07-21-2023 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)]; RE: Objective-C - Remove last character from string - unacademicmyrugntfcx - 07-21-2023 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:`][1] to delete the last character correctly. [1]: [To see links please register here] RE: Objective-C - Remove last character from string - filiatedoc - 07-21-2023 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: <br> if ([string length] > 0) { string = [string substringToIndex:[string length] - 1]; } else { //no characters to delete... attempting to do so will result in a crash } <br> <br><br><br> _______ 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: <br><br> If there is a character to delete (i.e. the length of the string is greater than 0) <br> `(string.length>0)` returns `1` thus making the code return: <br> `string = [string substringToIndex:string.length-1];` <br><br> If there is NOT a character to delete (i.e. the length of the string is NOT greater than 0) <br> `(string.length>0)` returns `0` thus making the code return: <br> `string = [string substringToIndex:string.length-0];` <br> Which prevents crashes. |