Remove first character from string if 0

4 Solutions Collect From Internet About “Remove first character from string if 0”

One solution could be:

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

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.

Swift 3.0:

var myString = "Hello, World"
myString.remove(at: myString.startIndex)

myString // "ello, World"

Swift 3

hasPrefix(“0”) check if the first character is 0 in txtField
and remove it

if (self.txtField.text?.hasPrefix("0"))! {