如何知道该字符串包含全零?
How to know that string contains all zeros?
我的应用程序中有一个呼叫选项,但有时我收到的 phone 号码类似于“000000000”。所以在那种情况下,我必须为用户禁用呼叫选项。
但是如何知道该字符串包含全零呢?
请帮我。提前致谢。
将所有的 0 替换为空,然后检查字符串长度是否为零。
NSString *phoneNumber = @"000000000";
NSString *testNumber = [phoneNumber stringByReplacingOccurencesOfString:@"0"
withString:@""];
if (testNumber.length == 0) {
// phoneNumber contains all zeroes.
}
else {
// phoneNumber may be good.
}
请试试这个,我认为它会对你有所帮助
NSString *stringToBeTested=@"0000000000";
NSString *mobileNumberPattern = @"^0*$";
NSPredicate *mobileNumberPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileNumberPattern];
BOOL matched = [mobileNumberPred evaluateWithObject:stringToBeTested];
if (matched){
//contain all zero
NSLog(@"sucess");
}else{
}
我的应用程序中有一个呼叫选项,但有时我收到的 phone 号码类似于“000000000”。所以在那种情况下,我必须为用户禁用呼叫选项。 但是如何知道该字符串包含全零呢? 请帮我。提前致谢。
将所有的 0 替换为空,然后检查字符串长度是否为零。
NSString *phoneNumber = @"000000000";
NSString *testNumber = [phoneNumber stringByReplacingOccurencesOfString:@"0"
withString:@""];
if (testNumber.length == 0) {
// phoneNumber contains all zeroes.
}
else {
// phoneNumber may be good.
}
请试试这个,我认为它会对你有所帮助
NSString *stringToBeTested=@"0000000000";
NSString *mobileNumberPattern = @"^0*$";
NSPredicate *mobileNumberPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileNumberPattern];
BOOL matched = [mobileNumberPred evaluateWithObject:stringToBeTested];
if (matched){
//contain all zero
NSLog(@"sucess");
}else{
}