xcode 检查字符串中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5305847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check for String within a String
提问by Lauren Quantrell
I'm trying to compare two strings
我正在尝试比较两个字符串
NSString strOne = @"Cat, Dog, Cow";
NSString strTwo = @"Cow";
How do I determine if strOne contains strTwo
如何确定 strOne 是否包含 strTwo
回答by fbrereto
Try using rangeOfString:
尝试使用 rangeOfString:
NSRange result = [strOne rangeOfString:strTwo];
From the documentation:
从文档:
Returns an
NSRange
structure giving the location and length in the receiver of the first occurrence ofaString
. Returns{NSNotFound, 0}
if aString is not found or is empty (@""
).
返回一个
NSRange
结构体,给出 的第一次出现在接收器中的位置和长度aString
。返回{NSNotFound, 0}
如果ASTRING找不到或为空(@""
)。
回答by Lauren Quantrell
For anyone needing the code to check is a string exists within a string, here's my code thanks to fbrereto. This example checks to see if any string contained in an array of strings (stringArray) can be found within a string (myString):
对于需要代码来检查字符串中是否存在字符串的任何人,感谢 fbrereto,这是我的代码。此示例检查是否可以在字符串 (myString) 中找到包含在字符串数组 (stringArray) 中的任何字符串:
int count = [stringArray count];
for (NSUInteger x = 0; x < count; ++x) {
NSRange range = [self.myString rangeOfString:[stringArray objectAtIndex:x]];
if (range.length > 0) {
// A match has been found
NSLog(@"string match: %@",[stringArray objectAtIndex:x]);
}
}
回答by Mooner
I believe this is the correct syntax for checking if the range exists (correcting response from Kendall):
range.location != NSNotFound
我相信这是检查范围是否存在的正确语法(更正来自 Kendall 的响应):
range.location != NSNotFound
回答by redux
Gradually straying off topic, but I always explode my strings, which would mean just exploding it using your search string as a key and you can use the array count to see how many instances you have.
逐渐偏离主题,但我总是分解我的字符串,这意味着只需使用您的搜索字符串作为关键字分解它,您可以使用数组计数来查看您有多少个实例。
Just incase anyone is coming from a code language that uses "explode" to blow a string up into an array like me, I found writing my own explode function tremendously helpful, those not using "explode" are missing out:
以防万一有人来自使用“爆炸”将字符串炸成数组的代码语言,我发现编写自己的爆炸函数非常有帮助,那些不使用“爆炸”的人错过了:
- (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;
}
works like this:
像这样工作:
[self explodeString: @"John Smith|Age: 37|Account Balance: .00" key:@"|"];
which will return this array:
这将返回这个数组:
[@"John Smith", @"Age: 37", @"Account Balance: .00"];
This lets you quickly pull out a specific value in a tight space, Like if you have a client and you want to know how much money he has:
这使您可以在狭窄的空间中快速提取特定值,例如,如果您有一个客户并且您想知道他有多少钱:
[[self explodeString: clientData key: pipe] objectAtIndex: 1];
or if you wanted specifically the dollar amount as a float:
或者,如果您特别想要将美元金额作为浮动金额:
[[[self explodeString: [[self explodeString: clientData key: pipe] objectAtIndex: 1] key: @": "] objectAtIndex: 2] floatValue];
anyway I find arrays way easier to work with and more flexible, so this is very helpful to me. Additionally with a little effort you could make an "explodable string" data type for your private library that lets you treat it like a string or return an index value based on the key
无论如何,我发现数组更易于使用且更灵活,因此这对我非常有帮助。此外,只需稍加努力,您就可以为您的私有库创建一个“可分解的字符串”数据类型,让您将其视为字符串或基于键返回索引值
ExplodableString *myExplodableString;
myExplodableString.string = @"This is an explodable|string";
NSString *secondValue = [myExplodableString useKey: @"|" toGetValue: index];