70

I have a string like this "A. rahul VyAs"

and i want to remove "A. " and the space after the "A." so that new string would be "rahul VyAs"

How do i achieve this?

4 Answers 4

252

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];
1
  • 12
    You should also check if there's at least 3 characters in the string. May 23, 2012 at 6:14
4

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.

13
  • This will only work if the string begins with "A. " and doesn't contain "A. " anywhere else. For instance, it won't work for "BlahA. Hello"
    – Fogmeister
    Apr 9, 2015 at 7:37
  • Ok, so I want to remove the first three characters from these two strings... "Hello, world!" And "Foo Bar". How will you do that with your solution?
    – Fogmeister
    Apr 9, 2015 at 7:40
  • @Fogmeister, read the update. The point is, if there is a common character set or something specific, as the question asked has stated A. , you can remove without a count. It's just another suggestion, no need to get frustrated :-) Apr 9, 2015 at 7:43
  • He doesn't say that there is a common beginning. He gives one example of something he'd like to do. The question title is how do I remove the first three letters. Not how do I remove "A. ".
    – Fogmeister
    Apr 9, 2015 at 7:45
  • @Fogmeister I answered how to remove the first three character, as asked. My answer removes his example of three characters. It also explains how to use it and when. It's another suggestion, and my update has clarified that it removes what you specifically state to remove. It really seems like your nit-picking. Apr 9, 2015 at 7:47
2

Try this,

char *string=[@"A. rahul VyAs" cStringUsingEncoding:NSUTF8StringEncoding];
char *subString=&name[3];
NSString *newString=[NSString stringWithCString:subString encoding:NSUTF8StringEncoding];
-1

It's this simple:

myString = [myString subStringFromIndex:3]

That's it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.