-2

我有一个这样的nsstring:

Variable1 = x;\n    Variable2 = y;\n    variable3 = z;\n    variable4 = t

为了提出一个明确的问题,我正在显示我的 nsstring 的 nslog:

NSLog(@"%@",myNsString);

在 NSLOG:

    Variable1 = x
    Variable2 = y
    variable3 = z
    variable4 = t

我想将它们分成不同的变量。

4

1 回答 1

2

所以听起来你想要单独的字符串中的 x、y、z 和 t 值?你把它们放在一个大串里?如果您需要帮助,请尽量说得更清楚,但这应该让您朝着正确的方向开始:

NSString *yourNSString = @"Variable1 = x;\n    Variable2 = y;\n    variable3 = z;\n    variable4 = t";

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"=;"];
NSArray *splitter = [yourNSString componentsSeparatedByCharactersInSet:set];
NSString *x;
NSString *y;
NSString *z;
NSString *t;

for(int i = 0; i<splitter.count ; i++)
{
    if(i==1)
    {
        x = [[splitter objectAtIndex:i]stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    else if(i==3)
    {
        y = [[splitter objectAtIndex:i]stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    else if(i==5)
    {
        z = [[splitter objectAtIndex:i]stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    else if(i==7)
    {
        t = [[splitter objectAtIndex:i]stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
}
于 2013-05-10T22:49:11.457 回答