CODE
文字列から部分文字列の値 ( X2.31
)を取得する必要があります
NSString *str = @"SHMU=\"\" CODE=\"X2.31\" XTN=\";
その特定の部分文字列を取得するにはどうすればよいですか?
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;
}
これは、投稿したstrにのみ固有です。あなたの番号の後に最初の X が続く必要があります。
float f=[[str componentsSeparatedByString:@"X"][1] floatValue];