Evgeniy Kleban Evgeniy Kleban - 4 months ago 23
iOS Question

Substring specific integer from string

I have google some related questions, but unfortunately didn't find answer.

I have string like

2016-07-22
, i need to get an integer
07
, evaluate it to 7 and save.

Of course, this is date, therefore it will change every time, so i cant suppose that year always will be 2016. I need to get string after 5th symbol up to 8th.

Is there any easy way to achieve that? Thanks.

Answer

If it's always that section of the string, you can use NSMakeRange(5, 2) and substringWithRange to pick out the month.

After you have "07", conversion is just a case of asking the string for its integerValue.

The "save" part depends entirely on where you want to save it.

Comments