【发布时间】:2011-08-17 11:55:06
【问题描述】:
我有一个类似Hello,How are you,Norman,Stanley,Fletcher 的 NSString,所以我想在出现逗号分隔符时拆分该字符串,并将该字符串设置为 iPhone 中的 UILabel。
我该怎么做?
【问题讨论】:
标签: iphone objective-c
我有一个类似Hello,How are you,Norman,Stanley,Fletcher 的 NSString,所以我想在出现逗号分隔符时拆分该字符串,并将该字符串设置为 iPhone 中的 UILabel。
我该怎么做?
【问题讨论】:
标签: iphone objective-c
原字符串:
NSString originalString = @"Hello,How are you,Norman,Stanley,Fletcher";
分成多行:
NSString multiLineString = [originalString stringByReplacingOccurrencesOfString:@"," withString:"@\n"];
分配给您的标签:
label.text = mutliLineString;
需要确保标签占用多行:
label.numberOfLines = 0;
将使其显示所需的行数。
【讨论】:
您可以使用componentsSeparatedByString: 方法来分割字符串。此方法将返回一个 NSStrings 数组。 您可以将它们显示在 UITextView 中,并将 editable 属性设置为 no。
【讨论】:
你可以像这样使用:
NSString * str = @"Hello,How are you,Norman,Stanley,Fletcher";
yourLabel.text = [str stringByReplacingOccurrencesOfString:@"," withString:@"\n"]);
【讨论】:
要拆分你的@“你好,你好吗”,请使用以下代码:
NSString *string = @"Hello,How are you";
NSArray *array = [string componentsSeparatedByString: @","];
要检查字符串中是否有逗号,请使用:
NSRange matchNotFound;
matchNotFound = [[label text] rangeOfString: @","];
if ((matchNotFound.location != NSNotFound) {
//if there is a comma
} else {
//if there is no comma
}
Hopping 觉得这很有用。
【讨论】: