你如何将NSString分成组件?

时间:2022-09-15 21:41:28

In Xcode, if I have an NSString containing a number, ie @"12345", how do I split it into an array representing component parts, ie "1", "2", "3", "4", "5"... There is a componentsSeparatedByString on the NSString object, but in this case there is no delimiter...

在Xcode中,如果我有一个包含数字的NSString,即@“12345”,我该如何将其拆分为表示组件部分的数组,即“1”,“2”,“3”,“4”,“5” ... NSString对象上有一个componentsSeparatedByString,但在这种情况下没有分隔符...

5 个解决方案

#1


4  

It may seem like characterAtIndex: would do the trick, but that returns a unichar, which isn't an NSObject-derived data type and so can't be put into an array directly. You'd need to construct a new string with each unichar.

它可能看起来像characterAtIndex:会做的伎俩,但返回一个unichar,它不是NSObject派生的数据类型,所以不能直接放入数组。你需要用每个unichar构造一个新的字符串。

A simpler solution is to use substringWithRange: with 1-character ranges. Run your string through a simple for (int i=0;i<[myString length];i++) loop to add each 1-character range to an NSMutableArray.

一个更简单的解决方案是使用substringWithRange:1个字符范围。通过一个简单的for(int i = 0; i <[myString length]; i ++)循环运行你的字符串,将每个1字符范围添加到NSMutableArray。

#2


24  

There is a ready member function of NSString for doing that:

有一个NSString的ready成员函数用于执行此操作:

NSString* foo = @"safgafsfhsdhdfs/gfdgdsgsdg/gdfsgsdgsd";
NSArray* stringComponents = [foo componentsSeparatedByString:@"/"];

#3


1  

A NSString already is an array of it’s components, if by components you mean single characters. Use [string length] to get the length of the string and [string characterAtIndex:] to get the characters.

NSString已经是它的组件数组,如果组件是指单个字符。使用[string length]获取字符串的长度,使用[string characterAtIndex:]获取字符。

If you really need an array of string objects with only one character you will have to create that array yourself. Loop over the characters in the string with a for loop, create a new string with a single character using [NSString stringWithFormat:] and add that to your array. But this usually is not necessary.

如果您确实需要一个只包含一个字符的字符串对象数组,则必须自己创建该数组。使用for循环遍历字符串中的字符,使用[NSString stringWithFormat:]创建一个包含单个字符的新字符串,并将其添加到数组中。但这通常没有必要。

#4


1  

In your case, since you have no delimiter, you have to get separate chars by

在你的情况下,由于你没有分隔符,你必须得到单独的字符

- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange

or this one

或者这个

- (unichar)characterAtIndex:(NSUInteger) index inside a loop.

That the only way I see, at the moment.

这是我看到的唯一方式。

#5


0  

Don't know if this works for what you want to do but:

不知道这是否适用于您想要做的事情但是:

const char *foo = [myString UTF8String]
char third_character = foo[2];

Make sure to read the docs on UTF8String

请务必阅读UTF8String上的文档

#1


4  

It may seem like characterAtIndex: would do the trick, but that returns a unichar, which isn't an NSObject-derived data type and so can't be put into an array directly. You'd need to construct a new string with each unichar.

它可能看起来像characterAtIndex:会做的伎俩,但返回一个unichar,它不是NSObject派生的数据类型,所以不能直接放入数组。你需要用每个unichar构造一个新的字符串。

A simpler solution is to use substringWithRange: with 1-character ranges. Run your string through a simple for (int i=0;i<[myString length];i++) loop to add each 1-character range to an NSMutableArray.

一个更简单的解决方案是使用substringWithRange:1个字符范围。通过一个简单的for(int i = 0; i <[myString length]; i ++)循环运行你的字符串,将每个1字符范围添加到NSMutableArray。

#2


24  

There is a ready member function of NSString for doing that:

有一个NSString的ready成员函数用于执行此操作:

NSString* foo = @"safgafsfhsdhdfs/gfdgdsgsdg/gdfsgsdgsd";
NSArray* stringComponents = [foo componentsSeparatedByString:@"/"];

#3


1  

A NSString already is an array of it’s components, if by components you mean single characters. Use [string length] to get the length of the string and [string characterAtIndex:] to get the characters.

NSString已经是它的组件数组,如果组件是指单个字符。使用[string length]获取字符串的长度,使用[string characterAtIndex:]获取字符。

If you really need an array of string objects with only one character you will have to create that array yourself. Loop over the characters in the string with a for loop, create a new string with a single character using [NSString stringWithFormat:] and add that to your array. But this usually is not necessary.

如果您确实需要一个只包含一个字符的字符串对象数组,则必须自己创建该数组。使用for循环遍历字符串中的字符,使用[NSString stringWithFormat:]创建一个包含单个字符的新字符串,并将其添加到数组中。但这通常没有必要。

#4


1  

In your case, since you have no delimiter, you have to get separate chars by

在你的情况下,由于你没有分隔符,你必须得到单独的字符

- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange

or this one

或者这个

- (unichar)characterAtIndex:(NSUInteger) index inside a loop.

That the only way I see, at the moment.

这是我看到的唯一方式。

#5


0  

Don't know if this works for what you want to do but:

不知道这是否适用于您想要做的事情但是:

const char *foo = [myString UTF8String]
char third_character = foo[2];

Make sure to read the docs on UTF8String

请务必阅读UTF8String上的文档