在我的程序中,我正在使用
获取HTML通知标记 "<script>p(34,'1','hello world');</script>/r/n"
我现在需要从上面的标记中获取字符串“hello world”,它应该是一个动态过程,因为每个响应标记的文本长度都不同。
答案 0 :(得分:2)
这样的东西会起作用。如果消息的那些部分也是动态的,你可能必须调整fromString和toString,但这会让你开始。
NSString *someString = @"<script>p(34,'1','hello world');</script>/r/n";
NSString *fromString = @"<script>p(34,'1','";
NSRange fromRange = [someString rangeOfString:fromString];
NSString *toString = @"');</script>/r/n";
NSRange toRange = [someString rangeOfString:toString];
NSRange range = NSMakeRange(fromRange.location + fromRange.length, toRange.location - (fromRange.location + fromRange.length));
NSString *result = [someString substringWithRange:range];
答案 1 :(得分:1)
NSString * myString = @"<script>p(34,'1','hello world');</script>/r/n";//@" hello(1234)";
NSRange range1 = [myString rangeOfString:@"," options:NSBackwardsSearch];
NSRange range2 = [myString rangeOfString:@"'" options:NSBackwardsSearch];
if ((range1.length == 1) && (range2.length == 1) && (range2.location > range1.location))
{
NSRange range3;
range3.location = range1.location+2;
range3.length = (range2.location - range1.location)-2;
NSString *subString = [myString substringWithRange:range3];
NSLog(@"%@",subString)
}
<强>输出----&GT;你好世界
答案 2 :(得分:1)
请尝试使用这个....我希望这项工作会很好......
NSString *data = @"<script>p(34,'1','hello world');</script>/r/n";
NSRange divRange = [data rangeOfString:@"','" options:NSCaseInsensitiveSearch];
if (divRange.location != NSNotFound)
{
NSRange endDivRange;
endDivRange.location = divRange.length + divRange.location;
endDivRange.length = [data length] - endDivRange.location;
endDivRange = [data rangeOfString:@"'" options:NSCaseInsensitiveSearch range:endDivRange];
if (endDivRange.location != NSNotFound)
{
divRange.location += divRange.length;
divRange.length = endDivRange.location - divRange.location;
NSLog(@"SubString %@",[data substringWithRange:divRange]);
}
}