0
1 US Dollar = 3.673 United Arab Emirates Dirham

如何3.673从此字符串中仅获取值?

我尝试了以下代码,但没有奏效。

NSString *a = [[xmlParser array]objectAtIndex:2];
NSRange startRange = [a rangeOfString:@"="];
NSLog(@"%u",startRange.location+1);
NSRange endRange = [a rangeOfString:@" "];
NSLog(@"%u",endRange.location);
NSString *subString = [a substringWithRange:NSMakeRange(startRange.location + 1, endRange.location)];
NSLog(@"%@",subString);
4

4 回答 4

4

用这个:

 NSArray *aArray = [a componentsSeparatedByString:@" "]; //your string here
 NSString *dollarValue = [aArray objectAtIndex:4]; //get dollar value from array
 NSLog(@"%@",dollarValue);
于 2012-10-10T11:18:31.010 回答
2

我会做类似...

NSArray *words = [string componentsSeparatedByString:@" "];

for (NSString *word in words) {
    //check for equals sign and then take the next one.
}
于 2012-10-10T11:18:41.810 回答
1
str = [NSString stringWithFormat:@"1 US Dollar = 3.673 United Arab Emirates Dirham"];  
components = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
str = [components objectAtIndex:4];
NSLog(@"Str:%@", str);
于 2012-10-10T11:26:31.923 回答
0
 NSString *param = nil;
    NSRange start = [a rangeOfString:@"="];
    if (start.location != NSNotFound)
    {
        param = [a substringFromIndex:start.location + start.length];
        NSRange end = [param rangeOfString:@"United"];
        if (end.location != NSNotFound)
        {
            param = [param substringToIndex:end.location];
        }
    }
    NSLog(@"param: %@",param);
于 2012-10-10T11:22:33.787 回答