Ios 打印文本中遇到的符号,无重复
我已经为这个问题挣扎了几天了。我真的需要你的帮助和意见 我们有一个字符串,它包含一个文本:Ios 打印文本中遇到的符号,无重复,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我已经为这个问题挣扎了几天了。我真的需要你的帮助和意见 我们有一个字符串,它包含一个文本: NSString *contentOfFile = [[NSString alloc] initWithString:@"This is string#1"]; 现在我必须记录符号,这些符号在这个字符串中没有重复。结果应该如下所示: whitespace symbol here # 1 g h i n r s t NSString *contentOfFile = [@"This is string#
NSString *contentOfFile = [[NSString alloc] initWithString:@"This is string#1"];
现在我必须记录符号,这些符号在这个字符串中没有重复。结果应该如下所示:
whitespace symbol here
#
1
g
h
i
n
r
s
t
NSString *contentOfFile = [@"This is string#1" lowercaseString];
我知道在C代码中使用字符集和迭代器可以非常简单地解决这个问题,但我正在寻找在objective-C中处理这个操作的同样简单而优雅的方法
我想在字符串上使用NSCharacterSet,但我对objective-c缺乏知识,所以我需要你们的帮助。提前感谢所有回复的人。//创建字符串
// Create the string
NSString *contentOfFile = @"This is string#1";
// Remove all whitespaces
NSString *whitespaceRemoval = [contentOfFile stringByReplacingOccurrencesOfString:@" " withString:@""];
// Initialize an array to store the characters
NSMutableArray *components = [NSMutableArray array];
// Iterate through the characters and add them to the array
for (int i = 0; i < [whitespaceRemoval length]; i++) {
NSString *character = [NSString stringWithFormat:@"%c", [whitespaceRemoval characterAtIndex:i]];
if (![components containsObject:character]) {
[components addObject:character];
}
}
NSString*contentOfFile=@“这是字符串#1”;
//删除所有空白
NSString*whitespaceremovation=[contentOffileStringByReplacingOccurrencesofString:@”“with String:@”“];
//初始化数组以存储字符
NSMutableArray*组件=[NSMutableArray];
//遍历字符并将其添加到数组中
对于(int i=0;i<[空白删除长度];i++){
NSString*character=[NSString stringWithFormat:@“%c”,[WhitespaceRemoving characterAtIndex:i]];
if(![组件包含对象:字符]){
[组件添加对象:字符];
}
}
利用NSSet的一个特点:其成员是不同的
NSString *contentOfFile = @"This is string#1";
NSMutableSet *set = [NSMutableSet set];
NSUInteger length = [contentOfFile length];
for (NSUInteger index = 0; index < length; index++)
{
NSString *substring = [contentOfFile substringWithRange:NSMakeRange(index, 1)];
[set addObject:substring];
}
NSLog(@"%@", set);
如果不区分大小写对您很重要,那么不幸的是NSSet没有“不区分大小写”选项。但是,您可以将源字符串转换为所有小写,如下所示:
whitespace symbol here
#
1
g
h
i
n
r
s
t
NSString *contentOfFile = [@"This is string#1" lowercaseString];
这将为您提供与示例输出完全匹配的结果。缺乏
方面的知识并不意味着您无法阅读
的文档。该链接指向NSMutableCharacterSet
的addCharactersInString:
方法的文档。这样,您的问题就可以在一个LOC中解决。关于如何打印字符集,有什么提示吗?好的建议。那么,你将如何让每一个角色都脱颖而出呢?循环遍历整个unicode字符集,并为每个字符集调用Characteristic Member?感谢您的回复。但在您的示例中,会出现重复的字符,而我只需要获取一次字符,而不是重复的字符。在添加对象之前,只需添加一个检查以查看数组是否包含该对象。请看我的最新答案。非常感谢你的回答,这正是我所需要的。比NSMutableCharacterSet好得多,因为我在打印它时遇到了麻烦。再次感谢你。