objective-c - 将 NSString 与 NSString 分开

标签 objective-c ios

我有一个这样的字符串:NSString *str =@"text |0|1|5|this is another text"; 我想在单独的字符串中使用 watch 组件,例如:

NSString *str1 =@"text";
NSString *str2 =@"0";
...

我如何使用完整的 NSString (str) 执行此操作。谢谢你的回答

最佳答案

您可以根据管道分隔符拆分此数据。您的结果将在 NSArray 中单独返回。然后您可以通过索引访问元素。

NSArray *strs = [str componentsSeparatedByString: @"|"]; 

例如

NSString *str1 = (NSString*)[strs objectAtIndex: 0];   // @"text "
NSString *str2 = (NSString*)[strs objectAtIndex: 1];   // @"0"

关于objective-c - 将 NSString 与 NSString 分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10413732/

相关文章:

ios - 如何弹出到不在堆栈中的特定 View Controller ?

ios - 自动布局问题 : iOS 7 vs iOS8

ios - 两个 NSDate 之间的天数

objective-c - 为什么我会收到此错误?

ios - -[__NSArrayM insertObject :atIndex:]: object cannot be nil when calling addChildBehavior:

ios - 如何将网页设置为PhoneGap startPage?

iOS ViewController 边界使用与 Xcode Interface Builder 中的设备相同的边界

ios - 表格中的第一个单元格始终为空。仅从第二个单元格显示 - objective-c

ios - 从 Objective-C 转换为 Swift (TestFlight)

iOS 8 registerForRemoteNotifications 正确实现放置