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?

Yucel Bayram
  • 1,653
  • 2
  • 22
  • 41
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256

4 Answers4

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];
Alex Rozanski
  • 37,815
  • 10
  • 68
  • 69
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.

App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
  • 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 09 '15 at 07: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 09 '15 at 07: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 :-) – App Dev Guy Apr 09 '15 at 07: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 09 '15 at 07: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. – App Dev Guy Apr 09 '15 at 07:47
  • OK, first off. I'm not getting frustrated nor am I nitpicking. I'm pointing out a very straight forward fact. Your code may be simple but it is very inflexible. It will only work in one, very specific case. Also, I was on my phone when commenting so I didn't notice that this question is actually 6 years old. It was answered 6 years ago, that answer was accepted and it now has 157 up votes. What was it you were trying to add? – Fogmeister Apr 09 '15 at 07:54
  • Also, your code will still not work for this... "A. rahul A. VyAs". – Fogmeister Apr 09 '15 at 07:58
  • @Fogmeister I made an alternative contribution instead of the generic count, which works well if the character reference is always changing and at the beginning of the string as asked. No one has asked the question "Remove the first 3 characters that are always the same". This solution works if the first three characters are consistently the same, like with xml <![CDATA[ for example. As an alternative, it's a worthy answer. – App Dev Guy Apr 09 '15 at 07:58
  • @Fogmeister here is the askers question: **"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?"** Have I answered the question? YES. – App Dev Guy Apr 09 '15 at 08:01
  • You have answered the question **"I have one specific string that I would like to remove 'A. ' from the beginning of."** you have answered a single possible case that is mentioned in the question. The question shows a name as an example. People have different names. Is it totally unbelievable that someone could have a name with "A. " in the middle? – Fogmeister Apr 09 '15 at 08:13
  • @Fogmeister you make a worthwhile point but the exact opposite argument can be made - he has not asked "remove the first 3 characters repeatedly" or anything of the likes. His question is "I want to remove the "A. " how do I achieve this?" and I answered it. – App Dev Guy Apr 09 '15 at 08:31
  • Fair enough. Maybe. But still it's a 6 year old question that already has an accepted answer. Anyway, I'm off now. Have fun. – Fogmeister Apr 09 '15 at 08:32
  • @Fogmeister, that's the beauty of App development, old questions are still relevant, and questions can have a variety of answers and interpretations, as we have demonstrated ;-) Take care – App Dev Guy Apr 09 '15 at 08:34
2

Try this,

char *string=[@"A. rahul VyAs" cStringUsingEncoding:NSUTF8StringEncoding];
char *subString=&name[3];
NSString *newString=[NSString stringWithCString:subString encoding:NSUTF8StringEncoding];
rakeshNS
  • 4,227
  • 4
  • 28
  • 42
-1

It's this simple:

myString = [myString subStringFromIndex:3]

That's it.

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Alex Zavatone
  • 4,106
  • 36
  • 54