How to remove first 3 characters from NSString?
Solution 1:
You can use the NSString
instance methods substringWithRange:
or substringFromIndex:
NSString *str = @"A. rahul VyAs";
NSString *newStr = [str substringWithRange:NSMakeRange(3, [str length]-3)];
or
NSString *str = @"A. rahul VyAs";
NSString *newStr = [str substringFromIndex:3];
Solution 2:
This is a solution I have seen specifically for removing regularly occurring prefixes and solving the answer to the question How do I remove "A. "?
NSString * name = @"A. rahul VyAs";
NSString * prefixToRemove = @"A. ";
name = [name stringByReplacingOccurrencesOfString:prefixToRemove withString:@""];
This code will remove what you tell it to remove/change if the character set exists, such as "A. ", even if the three characters (or more/less) are in the middle of the string.
If you wanted to remove rahul
, you can. It's diverse in that you specify exactly what you want removed or changed, and if it exists anywhere in the String, it will be removed or changed.
If you only want a certain specified number of characters removed from the front of the text that are always random or unknown, use the [string length] method as is the top answer.
If you want to remove or change certain characters that repeatedly appear, the method I have used will enable that, similar to Wordsearch on document editors.