Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何获取字符串的特定部分?_Iphone_Nsstring_Split - Fatal编程技术网

Iphone 如何获取字符串的特定部分?

Iphone 如何获取字符串的特定部分?,iphone,nsstring,split,Iphone,Nsstring,Split,我有一个字符串,比如: "This is test string http://www.google.com and it is working." 我只想获取链接(http://www.google.com)从上面的字符串。我怎样才能得到它?它应该是这样工作的: NSString *test = @"This is test string http://www.google.com and it is working."; NSString *string = [test stringBy

我有一个字符串,比如:

"This is test string http://www.google.com and it is working."

我只想获取链接(
http://www.google.com
)从上面的字符串。我怎样才能得到它?

它应该是这样工作的:

NSString *test = @"This is test string http://www.google.com and it is working.";

NSString *string = [test stringByAppendingString:@" "];

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"https?://[^ ]* "
                                                                       options:0
                                                                         error:&error];
NSArray *matches = [regex matchesInString:string
                                  options:0
                                    range:NSMakeRange(0, [string length])];

for (NSTextCheckingResult *match in matches) {
    NSRange matchRange = [match range];
    NSString *url = [string substringWithRange:matchRange];

    NSLog(@"Found URL: %@", url);
}
您可以在此处找到有关使用NSRegularExpression的更多信息:


查看NSString类文档-其中有许多方法可用于查找某些子字符串格式的位置、在delimeter上拆分字符串以及提取子字符串等

e、 g.在上面的示例中,如果要提取字符串中的任何嵌入url,可以首先使用以下方法拆分字符串:

NSArray *substrings = [myString componentsSeparatedByString:@" "];
然后在结果数组中,循环遍历它,并确定它是否具有“http”字符串:

for (int i=0;i<[substrings length];i++) {
   NSString aStr = [substrings objectAtIndex:i];
   if ([aStr rangeOfString:@"http"].location != NSNotFound) {
        NSLog(@"I found a http url:%@", aStr);
   }
}

for(int i=0;i这将像一个符咒一样工作:

NSString *totalString = @"This is test string http://www.google.com and it is working.";
NSLog(@"%@", totalString);

NSRange urlStart = [totalString rangeOfString: @"http"];
NSRange urlEnd = [totalString rangeOfString: @".com"];
NSRange resultedMatch = NSMakeRange(urlStart.location, urlEnd.location - urlStart.location + urlEnd.length);

NSString *linkString = [totalString substringWithRange:resultedMatch];

NSLog (@"%@", linkString);

“h t t p://www.google.com”和“h t t p://www.google.com”之间的区别是什么?您发布了相同的字符串…整个字符串在哪里?“这是测试字符串http:www.google.com,它正在工作”是一个完整的字符串所有您要获取的URL都以“.com”结尾吗?Thnx我的朋友,它工作得很好:-)请注意!您的URL必须以“http”开头,以“.com”结尾,如果它们以“.net”或“.info”或其他形式结尾,请编辑NSRange urlEnd。[子字符串计数]您自己是否编写过一行代码?