Delete last NSString character

I have some problems if you are trying to remove the last NSString character. I am new to Objective-C and I have no idea how to do this.

Could you guys light me up?

+64
objective-c nsstring
Oct 03 '11 at 23:00
source share
7 answers
NSString *newString = [oldString substringToIndex:[oldString length]-1]; 

Always refer to the documentation :




To include code relevant to your case:

 NSString *str = textField.text; NSString *truncatedString = [str substringToIndex:[str length]-1]; 
+173
Oct 03 2018-11-11T00:
source share

Try the following:

 s = [s substringToIndex:[s length] - 1]; 
+11
03 Oct 2018-11-11T00:
source share
 NSString *string = [NSString stringWithString:@"ABCDEF"]; NSString *newString = [string substringToIndex:[string length]-1]; NSLog(@"%@",newString); 

You can see = ABCDE

+4
May 7 '13 at
source share
 NSString = *string = @"abcdef"; string = [string substringToIndex:string.length-(string.length>0)]; 

If there is a character to delete (i.e. the length of the string is greater than 0) (string.length>0) returns 1, which returns the code:

  string = [string substringToIndex:string.length-1]; 

If there is no character to delete (i.e. the string is NOT longer than 0) (string.length>0) returns 0, which returns the code:

 string = [string substringToIndex:string.length-0]; 

which prevents crashes.

+3
Mar 05 '14 at 8:10
source share

This code will simply return the last character of the string and not delete it:

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

you can use this instead of removing the last character and saving the remaining string values:

 str = [str substringWithRange:NSMakeRange(0,[str length] - 1)]; 

and also using substringToIndex for NSString with 0 length will fail.

you must add confirmation before doing this:

 if ([str length] > 0) { str = [str substringToIndex:[s length] - 1]; } 

with this, it is safe to use the substring method.

NOTE. Apple will reject your application if it is vulnerable to crashes.

+1
Aug 28 '14 at 7:34
source share

This nice little code that I found by chance, and I have not yet seen it offered on Stack.

It only works if the characters you want to delete or change exist, which is convenient in many scenarios. If the / s character does not exist, it will not change your NSString:

 NSString = [yourString stringByReplacingOccurrencesOfString:@"YOUR CHARACTERS YOU WANT TO REMOVE" withString:@"CAN either be EMPTY or WITH TEXT REPLACEMENT"]; 

Here is how I use it:

 //declare what to look for NSString * suffixTorRemove = @"</p>"; NSString * prefixToRemove = @"<p>"; NSString * randomCharacter = @"</strong>"; NSString * moreRandom = @"<strong>"; NSString * makeAndSign = @"&"; //I AM INSERTING A VALUE FROM A DATABASE AND HAVE ASSIGNED IT TO returnStr returnStr = [returnStr stringByReplacingOccurrencesOfString:suffixTorRemove withString:@""]; returnStr = [returnStr stringByReplacingOccurrencesOfString:prefixToRemove withString:@""]; returnStr = [returnStr stringByReplacingOccurrencesOfString:randomCharacter withString:@""]; returnStr = [returnStr stringByReplacingOccurrencesOfString:moreRandom withString:@""]; returnStr = [returnStr stringByReplacingOccurrencesOfString:makeAndSign withString:@"&"]; //check the output NSLog(@"returnStr IS NOW: %@", returnStr); 

This one line is very simple to do three things in one:

  • Checks your string for the characters you want to find
  • You can replace or change them using any
  • Does not affect surrounding code

It looks like word search other than iOS

0
Apr 09 '15 at 6:45
source share

Simple and best approach

 [mutableString deleteCharactersInRange:NSMakeRange([myRequestString length]-1, 1)]; 
0
Nov 03 '15 at 11:55
source share



All Articles