如何将字符串转换为带分隔符的数组?

时间:2011-05-24 13:56:22

标签: iphone objective-c xcode nsstring nsarray

我有一个以下格式的字符串

myString = "cat+dog+cow"

我需要将每个用+ in分隔的字符串存储到一个数组中。 例如:

myArray[0] = cat
myArray[1] = dog
myArray[2] = cow

有谁能告诉我这样做的正确方法?

12 个答案:

答案 0 :(得分:15)

componentsSeparatedByString:拆分字符串并将结果返回数组。

NSArray *myArray = [myString componentsSeparatedByString:@"+"];

[myArray objectAtIndex:0];//cat
[myArray objectAtIndex:1];//dog
[myArray objectAtIndex:2];//cow

答案 1 :(得分:4)

试试这个..

NSArray *arr = [myString componentsSeparatedByString:@"-"];

[arr objectAtIndex:0];//Hai
[arr objectAtIndex:1];//Welcome

答案 2 :(得分:1)

使用NSArraycomponentsSeparatedByString:功能。

 NSString string = @"hai-welcome";
 NSArray myArray = [string componentsSeparatedByString:@"-"];

NSString* haiString = [myArray objectAtIndex:0];
NSString* welcomeString = [myArray objectAtIndex:1];

答案 3 :(得分:1)

这很简单..

NSString * test = @"Hello-hi-splitting-for-test";
NSArray * stringArray = [test componentsSeparatedByString:@"-"];

// Now stringArray will contain all splitted strings.. :)

希望这会有所帮助......

我不想使用数组然后迭代每个字符......

NSMutableString * splittedString = nil;

for(int i=0;i<test.length;i++){

    unichar character = [test characterAtIndex:0];
    if (character=='-') {

        if (splittedString!=nil) {
            NSLog(@"String component %@",splittedString);
            [splittedString release];
            splittedString = nil;
        }

    } else {
        if (splittedString==nil) {
            splittedString = [[NSMutableString alloc] init];                
        }
        [splittedString appendFormat:@"%C",character];
    }

}

if (splittedString!=nil) {
    NSLog(@"String last component %@",splittedString);
    [splittedString release];
    splittedString = nil;
}

多数人......

答案 4 :(得分:1)

NSArray *myWords = [myString componentsSeparatedByString:@"+"];

答案 5 :(得分:1)

你可以找到这个非常简单的

NSString *str = @"cat+dog+cow";
NSArray *arr = [str componentsSeparatedByString:@"+"];
NSLog(@"Array items %@",arr);

<强>输出:

数组项目 (     猫,     狗,     牛 )

答案 6 :(得分:0)

NSArray *strArray = [myString componentsSeparatedByString:@"-"];

firstString = [strArray objectAtIndex:0];//Hai
secondString = [strArray objectAtIndex:1];//Welcome

答案 7 :(得分:0)

NSArray *lines = [string componentsSeparatedByString:@"-"];

第一个值将存储在的第0个索引中,第二个值将存储在的第1个索引中。

为什么不是阵列?

答案 8 :(得分:0)

如果您正在处理字符串,这将是解决方案:

NSString *mySstring = @"hai-welcome";
NSMutableArray *anArray=[[NSMutableArray alloc] initWithArray:[componentsSeparatedByString: @"-"]];

每个单词将存储在0-n的相应位置。

试试这个。 :)

答案 9 :(得分:0)

如果您反对使用数组,可以考虑这个 -

NSString *accessMode, *message;

NSScanner *scanner = [NSScanner scannerWithString:@"hai-welcome"];
NSCharacterSet *hyphenSet = [NSCharacterSet characterSetWithCharactersInString:@"-"];

[scanner scanUpToCharactersFromSet:hyphenSet intoString:&accessMode];
[scanner scanCharactersFromSet:hyphenSet intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@""] intoString:&message];

答案 10 :(得分:0)

简单代码:

NSString *myString = [[NSString alloc] initWithString:@"cat+dog+cow"];
NSArray *resultArray = [tempString componentsSeparatedByString:@"+"];

NSLog(@"1. %@ 2. %@ 3. %@",[resultArray objectAtIndex:0],[resultArray objectAtIndex:1],[resultArray objectAtIndex:2]);

答案 11 :(得分:0)

试试这个:

public interface IMyInterface : IDisposable {} //IDisposable will be called by Ninject when object collected by GC

public class MyModule : NinjectModule
{
    public override void Load()
    {
        Bind<IMyInterface>().To<MyImplementation>();//creates new instance on every Get<> call
    }
}

var a = kernel.Get<IMyInterface>();
var b = kernel.Get<IMyInterface>();
var c = kernel.Get<IMyInterface>();

var abcList = kernel.GetCurrentInstances<IMyInterface>(); // list contains [a,b,c] instances, because they are not collected at this point