2016-07-01 63 views
0

我在我的應用程序中有一個通話選項,但有時候我收到的電話號碼就像「000000000」。所以在這種情況下,我必須禁用用戶的呼叫選項。 但如何知道該字符串包含全零? 請幫幫我。提前致謝。如何知道該字符串包含全零?

+1

遍歷字符串爲非零的檢查? – bi0phaz3

+0

你能舉一個小例子嗎? – Himanth

+0

uhh也許刪除所有零,然後將其保存到臨時字符串,然後比較長度? – Joshua

回答

4

全部替換爲0,然後檢查字符串長度是否爲零。

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

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

謝謝@Avi其工作正常 – Himanth

0

請試試這個,我認爲它會幫助你

NSString *[email protected]"0000000000"; 
NSString *mobileNumberPattern = @"^0*$"; 
NSPredicate *mobileNumberPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileNumberPattern]; 

BOOL matched = [mobileNumberPred evaluateWithObject:stringToBeTested]; 
if (matched){ 
    //contain all zero 
    NSLog(@"sucess"); 
}else{ 

} 
相關問題