In Objective-C for iOS, how would I remove the last character of a string using a button action?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 you want a fancy way of doing this in just one line of code you could write it as:
*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)returns1thus 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)returns0thus making the code return:string = [string substringToIndex:string.length-0];Which prevents crashes.