Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last character of NSString

I've got some trouble 'ere trying to remove the last character of an NSString. I'm kinda newbie in Objective-C and I have no idea how to make this work.

Could you guys light me up?

like image 225
markus Avatar asked Oct 03 '11 23:10

markus


People also ask

How do you remove the last character of a string in Objective C?

(string. length>0) returns 0 thus making the code return: string = [string substringToIndex:string. length-0];

Do I need to release NSString?

If you create an object using a method that begins with init, new, copy, or mutableCopy, then you own that object and are responsible for releasing it (or autoreleasing it) when you're done with it. If you create an object using any other method, that object is autoreleased, and you don't need to release it.

What does NSString mean?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.


2 Answers

NSString *newString = [oldString substringToIndex:[oldString length]-1]; 

Always refer to the documentation:

  • substringToIndex:
  • length

To include code relevant to your case:

NSString *str = textField.text; NSString *truncatedString = [str substringToIndex:[str length]-1]; 
like image 88
sidyll Avatar answered Sep 21 '22 17:09

sidyll


Try this:

s = [s substringToIndex:[s length] - 1]; 
like image 35
Mark Byers Avatar answered Sep 20 '22 17:09

Mark Byers