Xcode 检查字符串中的字符串
我想比较两个字符串Xcode 检查字符串中的字符串,xcode,substring,Xcode,Substring,我想比较两个字符串 NSString strOne = @"Cat, Dog, Cow"; NSString strTwo = @"Cow"; 如何确定strOne是否包含strTwo请尝试使用 从文件中: 返回一个NSRange结构,给出第一次出现的aString在接收器中的位置和长度。如果未找到aString或aString为空(@“),则返回{NSNotFound,0}) 试用 从文件中: 返回一个NSRange结构,给出第一次出现的aString在接收器中的位置和长度。如果未找到aSt
NSString strOne = @"Cat, Dog, Cow";
NSString strTwo = @"Cow";
如何确定strOne是否包含strTwo请尝试使用
从文件中:
返回一个NSRange
结构,给出第一次出现的aString
在接收器中的位置和长度。如果未找到aString或aString为空(@“
),则返回{NSNotFound,0}
)
试用
从文件中:
返回一个NSRange
结构,给出第一次出现的aString
在接收器中的位置和长度。如果未找到aString或aString为空(@“
),则返回{NSNotFound,0}
)
对于任何需要代码来检查字符串中是否存在字符串的人,这是我的代码,感谢fbrereto。此示例检查字符串数组(stringArray)中包含的任何字符串是否可以在字符串(myString)中找到:
int count=[stringArray count];
对于(整数x=0;x0){
//找到了匹配项
NSLog(@“字符串匹配:%@,[stringArray objectAtIndex:x]);
}
}
对于任何需要代码来检查字符串中是否存在字符串的人,这是我的代码,感谢fbrereto。此示例检查字符串数组(stringArray)中包含的任何字符串是否可以在字符串(myString)中找到:
int count=[stringArray count];
对于(整数x=0;x0){
//找到了匹配项
NSLog(@“字符串匹配:%@,[stringArray objectAtIndex:x]);
}
}
我认为这是检查范围是否存在的正确语法(更正Kendall的响应):
range.location!=NSNotFound
我认为这是检查范围是否存在的正确语法(更正Kendall的响应):
range.location!=NSNotFound
逐渐偏离主题,但我总是分解字符串,这意味着只需将搜索字符串作为键进行分解,您就可以使用数组计数来查看有多少实例
如果有人来自使用“explode”将字符串炸成数组的代码语言(如我),我发现编写自己的explode函数非常有帮助,而不使用“explode”的人则忽略了:
- (NSMutableArray *) explodeString : (NSString *)myString key:(NSString*) myKey
{
NSMutableArray *myArray = [[NSMutableArray alloc] init];
NSRange nextBreak = [myString rangeOfString:myKey];
while(nextBreak.location != NSNotFound)
{
[myArray addObject: [myString substringToIndex:nextBreak.location]];
myString = [myString substringFromIndex:nextBreak.location + nextBreak.length];
nextBreak = [myString rangeOfString:myKey];
}
if(myString.length > 0)
[myArray addObject:myString];
return myArray;
}
工作原理如下:
[self explodeString: @"John Smith|Age: 37|Account Balance: $75.00" key:@"|"];
它将返回此数组:
[@"John Smith", @"Age: 37", @"Account Balance: $75.00"];
这可以让您在狭小的空间内快速提取特定的值,例如,如果您有一个客户,您想知道他有多少钱:
[[self explodeString: clientData key: pipe] objectAtIndex: 1];
或者,如果您特别希望美元金额作为浮动:
[[[self explodeString: [[self explodeString: clientData key: pipe] objectAtIndex: 1] key: @": "] objectAtIndex: 2] floatValue];
无论如何,我发现数组更容易使用,也更灵活,所以这对我很有帮助。此外,只需稍加努力,您就可以为您的私有库创建一个“可分解字符串”数据类型,使您可以将其视为字符串或基于键返回索引值
ExplodableString *myExplodableString;
myExplodableString.string = @"This is an explodable|string";
NSString *secondValue = [myExplodableString useKey: @"|" toGetValue: index];
逐渐偏离主题,但我总是分解字符串,这意味着只需将搜索字符串作为键进行分解,就可以使用数组计数查看有多少实例 如果有人来自使用“explode”将字符串炸成数组的代码语言(如我),我发现编写自己的explode函数非常有帮助,而不使用“explode”的人则忽略了:
- (NSMutableArray *) explodeString : (NSString *)myString key:(NSString*) myKey
{
NSMutableArray *myArray = [[NSMutableArray alloc] init];
NSRange nextBreak = [myString rangeOfString:myKey];
while(nextBreak.location != NSNotFound)
{
[myArray addObject: [myString substringToIndex:nextBreak.location]];
myString = [myString substringFromIndex:nextBreak.location + nextBreak.length];
nextBreak = [myString rangeOfString:myKey];
}
if(myString.length > 0)
[myArray addObject:myString];
return myArray;
}
工作原理如下:
[self explodeString: @"John Smith|Age: 37|Account Balance: $75.00" key:@"|"];
它将返回此数组:
[@"John Smith", @"Age: 37", @"Account Balance: $75.00"];
这可以让您在狭小的空间内快速提取特定的值,例如,如果您有一个客户,您想知道他有多少钱:
[[self explodeString: clientData key: pipe] objectAtIndex: 1];
或者,如果您特别希望美元金额作为浮动:
[[[self explodeString: [[self explodeString: clientData key: pipe] objectAtIndex: 1] key: @": "] objectAtIndex: 2] floatValue];
无论如何,我发现数组更容易使用,也更灵活,所以这对我很有帮助。此外,只需稍加努力,您就可以为您的私有库创建一个“可分解字符串”数据类型,使您可以将其视为字符串或基于键返回索引值
ExplodableString *myExplodableString;
myExplodableString.string = @"This is an explodable|string";
NSString *secondValue = [myExplodableString useKey: @"|" toGetValue: index];
谢谢工作起来很有魅力。我在下面发布我的代码供其他人使用。谢谢!工作起来很有魅力。我将在下面发布我的代码供其他人使用。请注意,虽然该代码可以工作,但更好的检查方法是查看range.position!=NSNotFound(当找不到任何内容时将返回的常量值)。请注意,虽然该代码可以工作,但更好的检查是查看range.position!=NSNotFound(当找不到任何内容时将返回的常量值)。