开发者

String format compare in objective c

开发者 https://www.devze.com 2023-04-09 02:45 出处:网络
hello all i have one NSStringin this for开发者_C百科mat +1-123-123-1234 (phone number US base style) , my question is How can we compare that my string is formted in +1-123-123-1234 format .

hello all i have one NSString in this for开发者_C百科mat +1-123-123-1234 (phone number US base style) , my question is How can we compare that my string is formted in +1-123-123-1234 format .

on the basic of String compare formatted i have to done another task ..so any one have any idea regarding same please help me .


You can use a NSRegularExpression to test if your string is of a certain format.

NSRegularExpression *regex = [NSRegularExpression
    regularExpressionWithPattern:@"^\\+\\d-\\d{3}-\\d{3}-\\d{4}$"
                         options:NSRegularExpressionCaseInsensitive
                           error:nil];

int num = [regex numberOfMatchesInString:phoneNumberHere
                                 options:0
                                   range:NSMakeRange(0, [phoneNUmberHere length])];
if (num == 1) {
    // match!
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号