0

如何从原始字符串中删除字符串

即我的字符串是,

series0001.0001
or
series0010.0101
or
series0110.0050

从这个字符串我必须将它转换为

expected result

Series 1 1
Series 10 101
Series 110 50

这是我正在做的代码

    NSArray * words = [sym.data componentsSeparatedByString:@"\r\n\r\n"];
        NSLog(@"words are %@",words);
    NSString *strSeriesAndLabelDetail = [words objectAtIndex:0];
    NSLog(@"strSeriesAndLabelDetail is %@",strSeriesAndLabelDetail);

    NSArray *wordsToSeparateSeriesAndLabel = [strSeriesAndLabelDetail componentsSeparatedByString:@"."];
        NSLog(@"wordsSeriesLabel are %@",wordsToSeparateSeriesAndLabel);
    strLabelNumber = [wordsToSeparateSeriesAndLabel objectAtIndex:1];
    NSLog(@"strLabelNumber are %@",strLabelNumber);
    strSeriesNumber = [wordsToSeparateSeriesAndLabel objectAtIndex:0];

    NSLog(@"strSeriesNumber is %@",strSeriesNumber);

当前输出为:

words are (
    "series0001.0003",
    "Use the Sort-a-Cord app to read this code or visit www.sortacord.com to get your Sort-a-Cords."
)
strSeriesAndLabelDetail is series0001.0003
wordsSeriesLabel are (
    series0001,
    0003
)
strLabelNumber are 0003
strSeriesNumber is series0001

谁能帮帮我。提前感谢您的任何建议。

4

1 回答 1

1

一种方法:

NSString *stringToModify = @"series0110.0050";  
stringToModify = [stringToModify stringByReplacingOccurrencesOfString:@"series" withString:@""];
NSArray *array = [stringToModify componentsSeparatedByString:@"."];
NSString *finalString = [NSString stringWithFormat:@"Series %d %d", [[array objectAtIndex:0] integerValue], [[array objectAtIndex:1] integerValue]];
NSLog(@"%@",finalString);

请注意,它可能会根据您真正想要的内容进行修改。我假设您总是要寻找“系列”。

于 2013-07-10T11:10:35.950 回答