在NSString中查找动态单词

时间:2013-11-25 09:51:43

标签: ios iphone objective-c nsstring

NSString * stringExample1=@"www.mysite.com/word-4-word-1-1-word-word-2-word-817061.html";

NSString * stringExample2=@"www.mysite.com/word-4-5-1-1-word-1-5-word-11706555.html";

我尝试在-内找到.NSString

NSRange range = [string rangeOfString:@"-"];
NSUInteger start = range.location;
NSUInteger end = start + range.length;

NSRange rangeDot= [string rangeOfString:@"."];
NSUInteger startt = rangeDot.location;
NSUInteger endt = startt + rangeDot.length;

但这不可能成功。它显示了第一名。如何在817061内获取11706555Nstring

谢谢。

4 个答案:

答案 0 :(得分:4)

这对你有用,

 NSArray *strArry=[stringExample1 componentsSeparatedByString:@"-"];

 NSString *result =[strArry lastObject];

 NSString *resultstring= [result  stringByReplacingOccurrencesOfString:@".html" withString:@""];

答案 1 :(得分:0)

我认为找到匹配的最佳方法是使用带有 NSRegularExpression 的常规表达式。

NSString * stringEx=@"www.mysite.com/word-4-word-1-1-word-word-2-word-817061.html";

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"-(\\d*).html$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                        error:&error];

NSArray *matches = [regex matchesInString:stringEx options:NSMatchingReportCompletion range:NSMakeRange(0, [stringEx length])];
if ([matches count] > 0)
{
    NSString* resultString = [stringEx substringWithRange:[matches[0] rangeAtIndex:1]];
    NSLog(@"Matched: %@", resultString);
}

确保在需要时在正则表达式\中使用额外的NSString转义字符。

<强>更新

我使用以下代码使用两种不同的方法(正则表达式与字符串拆分)进行了测试:

NSDate *timeBefore = [NSDate date];

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"-(\\d*).html$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                        error:&error];

for (int i = 0; i < 100000; i++)
{
    NSArray *matches = [regex matchesInString:stringEx options:NSMatchingReportCompletion range:NSMakeRange(0, [stringEx length])];
    if ([matches count] > 0)
    {
        NSString* resultString = [stringEx substringWithRange:[matches[0] rangeAtIndex:1]];
    }
}

NSTimeInterval timeSpent = [timeBefore timeIntervalSinceNow];
NSLog(@"Time: %.5f", timeSpent*-1);

在模拟器上差异不显着,但在iPhone 4上运行我得到了以下结果:

2013-11-25 10:24:19.795 NotifApp[406:60b] Time: 11.45771 // string splitting
2013-11-25 10:25:10.451 NotifApp[412:60b] Time: 7.55713 // regex

所以我想最好的方法取决于具体情况。

答案 2 :(得分:0)

您是否尝试查找是否至少包含 - 中的一个?

您可以使用-rangeOfCharacterFromSet:

NSCharacterSet *CharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"-."];
NSRange range = [YourString rangeOfCharacterFromSet:CharacterSet];
if (range.location == NSNotFound) 
{
    // no - or . in the string
}
else 
{
    // - or . are present
}

答案 3 :(得分:0)

试试这个简单的正则表达式。

    NSString * stringExample1=@"www.mysite.com/word-4-word-1-1-word-word-2-word-84354354353.html";
    NSError  *error  = NULL;

    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:@"(\\-\\d*\\.)"
                                  options:0
                                  error:&error];

    NSRange range              = [regex rangeOfFirstMatchInString:stringExample1
                                               options:0
                                                 range:NSMakeRange(0, [stringExample1 length])];
    range                      = NSMakeRange(range.location+1, range.length-2);
    NSString *result           = [stringExample1 substringWithRange:range];
    NSLog(@"%@",result);