Pregunta

I am trying to remove the front character of a NSMutableString and have tried:

    [str setString: [str substringFromIndex:1]] ,
    [str deleteCharactersInRange:NSMakeRange(0,1)] 

and a few others. These two either remove every character or every character except for the one I am trying to remove so for example if I have the String "-2357" and am trying to remove the "-" then I get just the "-" when I should get "2357".

Am I doing something wrong, and how can I fix this?

¿Fue útil?

Solución 2

instead of:

[str substringToIndex:1]

Do this:

[str substringFromIndex:1]

Otros consejos

If you want to use the deleteCharactersInRange method, do this

NSRange range = {0,1};
[str deleteCharactersInRange:range];

Or, you could use substringFromIndex like so:

str = [str substringFromIndex:1];


Contrary to what you are saying, [str deleteCharactersInRange:NSMakeRange(0,1)] should work fine, I don't know why that is not working.

[str substringToIndex:1] will return the string from the beginning to the index 1, which is -. What you want is substringFromIndex. And as this method returns a NSString, you will need to assign it to str; just calling the method on str is not enough.


Edit

Using the deleteCharactersInRange may pose problems for some type of characters. For more information refer to this a question here.

Instead, you can safely use

[str deleteCharactersInRange:[str rangeOfComposedCharacterSequenceAtIndex:0]]

If you want to remove first character of the string you can use this:-

string = [string substringFromIndex:1];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top