从NSString提取子字符串 [英] extract substring from NSString
问题描述
我想从NSString中提取子字符串.怎么可能.
I wants to extract substring from NSString. How it is possible.
示例:字符串为 abcdefwww.google.comabcdef
输出: www.google.com
在主字符串中,不是预定义哪个url来,可能是google.com或yahoo.com等.
In the main String, It is not predefined which url comes, It may be google.com or yahoo.com etc.
所以我必须在 www 中找到w的开始位置,并在 .com 中找到m的最后位置.
So I have to find the Start location of w in www and the the last location of m in .com.
所以我的问题是,如何在 www 中找到 w 的开始位置,以及在中找到 m 的位置.com ,所以我提取了这一部分
So my question is that how I can find the start location of w in www and the location of m in .com, so i extract this part
由于问题而
我们如何从asdfwww.mywebsite.co.ukasdfajsf这个字符串中提取www.mywebsite.co.uk
How we can extract www.mywebsite.co.uk from this string asdfwww.mywebsite.co.ukasdfajsf
谢谢
推荐答案
像波纹管这样的字符串中的子字符串
substring from string like bellow
- (NSString *)extractString:(NSString *)fullString toLookFor:(NSString *)lookFor skipForwardX:(NSInteger)skipForward toStopBefore:(NSString *)stopBefore
{
NSRange firstRange = [fullString rangeOfString:lookFor];
NSRange secondRange = [[fullString substringFromIndex:firstRange.location + skipForward] rangeOfString:stopBefore];
NSRange finalRange = NSMakeRange(firstRange.location + skipForward, secondRange.location + [stopBefore length]);
return [fullString substringWithRange:finalRange];
}
使用如下所示的方法...
use this method like below...
NSString *strTemp = [self extractString:@"abcdefwww.google.comabcdef" toLookFor:@"www" skipForwardX:0 toStopBefore:@".com"];
NSLog(@"\n\n Substring ==>> %@",strTemp);
有关更多信息,请参见下面的链接.
for more information see bellow link..
help-needed-function-extract-string-string
这篇关于从NSString提取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!