himanth himanth - 5 months ago 7
Objective-C Question

How to know that string contains all zeros?

I have a call option in my app but sometimes I am getting phone number is like "000000000" . So at that case I have to disable call option for user.
But how to know that string contains all zeros?
Please help me. Thanks in advance.

Avi Avi
Answer

Replace all 0's with nothing and then check if the string length is zero.

NSString *phoneNumber = @"000000000";
NSString *testNumber = [phoneNumber stringByReplacingOccurencesOfString:@"0"
                                                             withString:@""];

if (testNumber.length == 0) {
    // phoneNumber contains all zeroes.
}
else {
    // phoneNumber may be good.
}