Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 642 Vote(s) - 3.6 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Objective-C - Remove last character from string

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

#2
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];
...
Reply

#3
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)];
Reply

#4
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]

Reply

#5
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through