如何检查字符串(NSString
)是否包含另一个较小的字符串?
我希望有类似的东西:
NSString *string = @"hello bla bla";
NSLog(@"%d",[string containsSubstring:@"hello"]);
但我能找到的最接近的是:
if ([string rangeOfString:@"hello"] == 0) {
NSLog(@"sub string doesnt exist");
}
else {
NSLog(@"exists");
}
无论如何,这是查找字符串是否包含另一个字符串的最佳方法吗?
答案 0 :(得分:2411)
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
NSLog(@"string does not contain bla");
} else {
NSLog(@"string contains bla!");
}
如果“haystack”不包含“needle”,则注意rangeOfString:
返回NSRange
结构,而the documentation says返回结构{NSNotFound, 0}
如果您使用的是iOS 8或OS X Yosemite,您现在可以:(*注意:如果在iOS7设备上调用此代码,这将使您的应用程序崩溃)。
NSString *string = @"hello bla blah";
if ([string containsString:@"bla"]) {
NSLog(@"string contains bla!");
} else {
NSLog(@"string does not contain bla");
}
(这也是它在Swift中的工作方式)
答案 1 :(得分:158)
注意:此答案现已过时
为NSString创建一个类别:
@interface NSString ( SubstringSearch )
- (BOOL)containsString:(NSString *)substring;
@end
// - - - -
@implementation NSString ( SubstringSearch )
- (BOOL)containsString:(NSString *)substring
{
NSRange range = [self rangeOfString : substring];
BOOL found = ( range.location != NSNotFound );
return found;
}
@end
编辑:观察Daniel Galasko关于命名的以下评论
答案 2 :(得分:51)
由于这似乎是Google的高排名结果,我想补充一下:
iOS 8和OS X 10.10将containsString:
方法添加到NSString
。这些系统的Dave DeLong示例的更新版本:
NSString *string = @"hello bla bla";
if ([string containsString:@"bla"]) {
NSLog(@"string contains bla!");
} else {
NSLog(@"string does not contain bla");
}
答案 3 :(得分:39)
NSString *myString = @"hello bla bla";
NSRange rangeValue = [myString rangeOfString:@"hello" options:NSCaseInsensitiveSearch];
if (rangeValue.length > 0)
{
NSLog(@"string contains hello");
}
else
{
NSLog(@"string does not contain hello!");
}
//您也可以使用以下内容:
if (rangeValue.location == NSNotFound)
{
NSLog(@"string does not contain hello");
}
else
{
NSLog(@"string contains hello!");
}
答案 4 :(得分:22)
使用 iOS 8 和 Swift ,我们可以使用localizedCaseInsensitiveContainsString
方法
let string: NSString = "Café"
let substring: NSString = "É"
string.localizedCaseInsensitiveContainsString(substring) // true
答案 5 :(得分:13)
所以我个人非常讨厌NSNotFound
,但要明白其必要性。
但有些人可能不理解与NSNotFound比较的复杂性
例如,此代码:
- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
if([string rangeOfString:otherString].location != NSNotFound)
return YES;
else
return NO;
}
有问题:
1)显然,otherString = nil
此代码会崩溃。一个简单的测试是:
NSLog(@"does string contain string - %@", [self doesString:@"hey" containString:nil] ? @"YES": @"NO");
结果!!撞死!!
2)对于Objective-c的新手来说,不那么明显的是string = nil
时相同的代码不会崩溃。
例如,此代码:
NSLog(@"does string contain string - %@", [self doesString:nil containString:@"hey"] ? @"YES": @"NO");
和这段代码:
NSLog(@"does string contain string - %@", [self doesString:nil containString:nil] ? @"YES": @"NO");
将导致
does string contains string - YES
这显然不是你想要的。
所以我认为更好的解决方案是使用rangeOfString返回长度为0的事实,这样一个更可靠的代码是这样的:
- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
if(otherString && [string rangeOfString:otherString].length)
return YES;
else
return NO;
}
或简单:
- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
return (otherString && [string rangeOfString:otherString].length);
}
将针对案例1和2返回
does string contains string - NO
那是我的2美分; - )
请查看我的Gist以获取更多有用的代码。
答案 6 :(得分:12)
NSString上的一个类别P i's solution的改进版本,它不仅可以告诉我,如果在另一个字符串中找到一个字符串,又通过引用获取范围,那么:
@interface NSString (Contains)
-(BOOL)containsString: (NSString*)substring
atRange:(NSRange*)range;
-(BOOL)containsString:(NSString *)substring;
@end
@implementation NSString (Contains)
-(BOOL)containsString:(NSString *)substring
atRange:(NSRange *)range{
NSRange r = [self rangeOfString : substring];
BOOL found = ( r.location != NSNotFound );
if (range != NULL) *range = r;
return found;
}
-(BOOL)containsString:(NSString *)substring
{
return [self containsString:substring
atRange:NULL];
}
@end
使用它像:
NSString *string = @"Hello, World!";
//If you only want to ensure a string contains a certain substring
if ([string containsString:@"ello" atRange:NULL]) {
NSLog(@"YES");
}
// Or simply
if ([string containsString:@"ello"]) {
NSLog(@"YES");
}
//If you also want to know substring's range
NSRange range;
if ([string containsString:@"ello" atRange:&range]) {
NSLog(@"%@", NSStringFromRange(range));
}
答案 7 :(得分:8)
以下是复制粘贴功能代码段:
-(BOOL)Contains:(NSString *)StrSearchTerm on:(NSString *)StrText
{
return [StrText rangeOfString:StrSearchTerm
options:NSCaseInsensitiveSearch].location != NSNotFound;
}
答案 8 :(得分:6)
Oneliner(代码量较少。干,因为你只有一个NSLog
):
NSString *string = @"hello bla bla";
NSLog(@"String %@", ([string rangeOfString:@"bla"].location == NSNotFound) ? @"not found" : @"cotains bla");
答案 9 :(得分:6)
NSString *categoryString = @"Holiday Event";
if([categoryString rangeOfString:@"Holiday"].location == NSNotFound)
{
//categoryString does not contains Holiday
}
else
{
//categoryString contains Holiday
}
答案 10 :(得分:5)
最佳解决方案。就这么简单!如果你想找一个单词或 字符串的一部分。您可以使用此代码。在这个例子中,我们将检查word的值是否包含" acter"。
NSString *word =@"find a word or character here";
if ([word containsString:@"acter"]){
NSLog(@"It contains acter");
} else {
NSLog (@"It does not contain acter");
}
答案 11 :(得分:5)
试试这个,
NSString *string = @"test Data";
if ([[string lowercaseString] rangeOfString:@"data"].location == NSNotFound)
{
NSLog(@"string does not contain Data");
}
else
{
NSLog(@"string contains data!");
}
答案 12 :(得分:4)
在Swift 4中:
let a = "Hello, how are you?"
a.contains("Hello") //will return true
答案 13 :(得分:4)
如果是swift,可以使用
let string = "Package #23"
if string.containsString("Package #") {
//String contains substring
}
else {
//String does not contain substring
}
答案 14 :(得分:4)
如果你需要这一次写:
NSString *stringToSearchThrough = @"-rangeOfString method finds and returns the range of the first occurrence of a given string within the receiver.";
BOOL contains = [stringToSearchThrough rangeOfString:@"occurence of a given string"].location != NSNotFound;
答案 15 :(得分:2)
如果不打扰区分大小写的字符串。 试一试。
NSString *string = @"Hello World!";
if([string rangeOfString:@"hello" options:NSCaseInsensitiveSearch].location !=NSNotFound)
{
NSLog(@"found");
}
else
{
NSLog(@"not found");
}
答案 16 :(得分:1)
使用选项NSCaseInsensitiveSearch和rangeOfString:options:
NSString *me = @"toBe" ;
NSString *target = @"abcdetobe" ;
NSRange range = [target rangeOfString: me options: NSCaseInsensitiveSearch];
NSLog(@"found: %@", (range.location != NSNotFound) ? @"Yes" : @"No");
if (range.location != NSNotFound) {
// your code
}
找到输出结果:是
选项可以是"或'"一起并包括:
NSCaseInsensitiveSearch NSLiteralSearch NSBackwardsSearch等等
答案 17 :(得分:1)
请使用此代码
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound)
{
NSLog(@"string does not contain bla");
}
else
{
NSLog(@"string contains bla!");
}
答案 18 :(得分:0)
第一个字符串包含或不包含第二个字符串
NSString *first = @"Banana";
NSString *second = @"BananaMilk";
NSRange range = [first rangeOfString:second options:NSCaseInsensitiveSearch];
if (range.length > 0) {
NSLog(@"Detected");
}
else {
NSLog(@"Not detected");
}
答案 19 :(得分:0)
SWift 4及以上
let str = "Hello iam midhun"
if str.contains("iam") {
//contain substring
}
else {
//doesn't contain substring
}
答案 20 :(得分:0)
尝试一下:
Swift 4.1、4.2:
let stringData = "Black board"
//swift quick way and case sensitive
if stringData.contains("bla") {
print("data contains string");
}
//case sensitive
if stringData.range(of: "bla",options: .caseInsensitive) != nil {
print("data contains string");
}else {
print("data does not contains string");
}
对于Objective-C:
NSString *stringData = @"Black board";
//Quick way and case sensitive
if ([stringData containsString:@"bla"]) {
NSLog(@"data contains string");
}
//Case Insensitive
if ([stringData rangeOfString:@"bla" options:NSCaseInsensitiveSearch].location != NSNotFound) {
NSLog(@"data contains string");
}else {
NSLog(@"data does not contain string");
}
答案 21 :(得分:-1)
hoverable
答案 22 :(得分:-2)
如果需要字符串的某个位置,此代码将放在 Swift 3.0 中:
input