Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first character from string if 0

I need to remove the first character from my UITextfield if it's a 0.

Unfortunately I don't know how to extract the value of the first character or extract the characters of the string after the first character.

Thanks

like image 857
user1923975 Avatar asked Dec 25 '12 14:12

user1923975


2 Answers

One solution could be:

if ([string hasPrefix:@"0"] && [string length] > 1) {     string = [string substringFromIndex:1]; } 
like image 195
DrummerB Avatar answered Sep 22 '22 06:09

DrummerB


You would probably want something like this, using hasPrefix:

if ([string hasPrefix:@"0"]) {     string = [string substringFromIndex:1]; } 

You could also use characterAtIndex: which returns a unichar:

if ([string characterAtIndex:0] == '0') {      string = [string substringFromIndex:1]; } 

Note that, 'a' is character, "a" is C string and @"a" is NSString. They all are different types.

like image 30
johankj Avatar answered Sep 22 '22 06:09

johankj