我需要从字符串
获取子串CODE
的值(X2.31
)
NSString *str = @"SHMU=\"\" CODE=\"X2.31\" XTN=\";
我怎样才能获得特定的子字符串?
答案 0 :(得分:1)
尝试以下
NSString *str = @"SHMU=\"\" CODE=\"X2.31\" XTN=\"";
NSRange range = [str rangeOfString:@"CODE="];
NSString *substring = [[str substringFromIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *str1 = [substring componentsSeparatedByString:@" "];
NSLog(@"the sub %@",[[str1 objectAtIndex:0] stringByTrimmingCharactersInSet[NSCharacterSet whitespaceCharacterSet]]);
字符串是“X2.31”
答案 1 :(得分:0)
NSString *strOrg = [NSString stringWithFormat:@"%@",@"SHMU=\"\" CODE=\"X2.31\" XTN=\""] ;
NSString *strLeft = [NSString stringWithFormat:@"%@",@"SHMU=\"\" CODE=\""] ;
NSString *strRight = [NSString stringWithFormat:@"%@",@"\" XTN=\""] ;
NSLog(@"%@", [self getDataBetweenString:strOrg LeftString:strLeft RightString:strRight LeftOffset:13]);
- (NSString *)getDataBetweenString:(NSString *)orgString LeftString:(NSString *)leftString RightString:(NSString *)rightString LeftOffset:(NSInteger)leftPos;
{
NSInteger left, right;
NSString *foundData;
NSScanner *scanner=[NSScanner scannerWithString:orgString];
[scanner scanUpToString:leftString intoString: nil];
left = [scanner scanLocation];
[scanner setScanLocation:left + leftPos];
[scanner scanUpToString:rightString intoString: nil];
right = [scanner scanLocation] + 1;
left += leftPos;
foundData = [orgString substringWithRange: NSMakeRange(left, (right - left) - 1)]; return foundData;
}
答案 2 :(得分:0)
这只是您发布的 str 的特定。 您的号码必须紧跟First X.
float f=[[str componentsSeparatedByString:@"X"][1] floatValue];