IOS:检查NSString的一部分
我有这个字符串:IOS:检查NSString的一部分,ios,xcode,nsstring,Ios,Xcode,Nsstring,我有这个字符串: NSString *test = @"three is a number"; 我想检查此NSString是否以“三”开头尝试使用hasPrefix方法: if([test hasPrefix:@"three"]) { // Begins with three } 这是我自己做的 if([test hasPrefix:@"three"]){ ... } 一般回答:无论何时,只要您想检查NSString(或苹果提供的任何类),请查阅官方文档!在本例中,请参见。你看,互
NSString *test = @"three is a number";
我想检查此NSString是否以“三”开头尝试使用hasPrefix方法:
if([test hasPrefix:@"three"])
{
// Begins with three
}
这是我自己做的
if([test hasPrefix:@"three"]){
...
}
一般回答:无论何时,只要您想检查
NSString
(或苹果提供的任何类),请查阅官方文档!在本例中,请参见。你看,互联网上不仅有StackOverflow,还有官方文件 对于更一般的方法,获取相关的子字符串,并将其与所需结果进行比较
if ([[test substringWithRange:NSMakeRange(0,5)] caseInsensitiveCompare:@"three"] == NSOrderedSame) {
// ....
}
您需要使用以下各项:
if ([test hasPrefix:@"three"])
{
// if YES
}