查找数组中的元素
我有以下代码:
NSArray *myArray = [NSArray arrayWithObjects: @"e", @"è", @"é",@"i","ò",nil];
NSString *string = @"simpleè";
NSMutablestring *newString;
for(i=0>;i< [string length]; i++){
if([stringa characterAtIndex:i] is in Array){
[newString appendFormat:@"%c", [string characterAtIndex:i]];
}
}
如何查找字符串的单个字符是否留在数组中?
结果示例:
newString = @“ieè”;
I have the follow code:
NSArray *myArray = [NSArray arrayWithObjects: @"e", @"è", @"é",@"i","ò",nil];
NSString *string = @"simpleè";
NSMutablestring *newString;
for(i=0>;i< [string length]; i++){
if([stringa characterAtIndex:i] is in Array){
[newString appendFormat:@"%c", [string characterAtIndex:i]];
}
}
How make finding if single char of string stay in the array?
Example of result:
newString= @"ieè";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想你想申请 rangeOfCharacterFromSet:options:range: 重复。您必须创建一个 NSCharacterSet 来自数组中的字符。
添加
尽管使用
characterAtIndex
循环遍历字符串并将每个字符(在内部循环中)与数组中的字符进行比较(您可以将其提取到unichar
数组或放入单个NSString
中以方便访问)。I think you want to apply rangeOfCharacterFromSet:options:range: repeatedly. You'll have to create a NSCharacterSet from the characters in your array somehow.
Added
Though it probably would be just as simple to just loop through the string with
characterAtIndex
and compare each char (in an inner loop) to the chars in your array (which you could extract into aunichar
array or put into a singleNSString
to make easier to access).嗯...如果你想检查值是什么,你可以使用 NSLog
所以你可以用它来检查你的数组...这就是我认为你所要求的,但是你问题中的语法不是很好。请提供更多细节和更好的语法。
Umm... if you want to check what the values are you can use NSLog
So you can use this to check your array... Which is what I think you are asking for, but the grammar in your question isn't very good. Please provide more details and better grammar.
您应该检查字符串的长度,然后将字符串字符与数组进行匹配,如果找到,则将该字符附加到新字符串中。
希望有帮助......
You should check the length of your string and then match your string characters with the array and if found append that character in a new string.
Hope it helps.......
您需要创建一个 NSCharacterSet 与字符串中的字符,然后询问数组中的每个字符串其
rangeOfCharacterFromSet:
。如果您找到实际找到范围的位置,则字符串中的一个字符位于数组中。如果不是,那么他们都不是。这可能看起来有点迂回,但 Unicode 使得将字符串视为一系列“字符”相当不可靠,并且您会希望让您的库尽可能为您完成繁重的工作。
You'll want to create an NSCharacterSet with the characters in the string and then ask each string in the array for its
rangeOfCharacterFromSet:
. If you find one where a range was actually found, then a character from the string is in the array. If not, then none of them are.This might seem a bit roundabout, but Unicode makes looking at strings as just a series of "chars" rather unreliable, and you'll want to let your libraries do as much of the heavy lifting for you as they can.
有更好的方法可以做到这一点,但这就是我认为你想要做的:
There are better ways to do this, but this is what I think you're trying to do: