查找数组中的元素

发布于 2024-12-11 18:46:02 字数 418 浏览 0 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鼻尖触碰 2024-12-18 18:46:02

我想你想申请 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 a unichar array or put into a single NSString to make easier to access).

薄凉少年不暖心 2024-12-18 18:46:02

嗯...如果你想检查值是什么,你可以使用 NSLog

NSLog"%f", myFloat;

所以你可以用它来检查你的数组...这就是我认为你所要求的,但是你问题中的语法不是很好。请提供更多细节和更好的语法。

Umm... if you want to check what the values are you can use NSLog

NSLog"%f", myFloat;

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.

放血 2024-12-18 18:46:02

您应该检查字符串的长度,然后将字符串字符与数组进行匹配,如果找到,则将该字符附加到新字符串中。

 NSString *mstr = @"asdf";
  NSString *b = [ mstr characterAtIndex:0];  

希望有帮助......

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.

 NSString *mstr = @"asdf";
  NSString *b = [ mstr characterAtIndex:0];  

Hope it helps.......

海未深 2024-12-18 18:46:02

您需要创建一个 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.

大姐,你呐 2024-12-18 18:46:02

有更好的方法可以做到这一点,但这就是我认为你想要做的:

NSMutableString* result= [NSMutableString stringWithString:@""];

for( int i= 0; i < [string length]; ++i ) {

    NSString* c= [NSString stringWithFormat:@"%C", [string characterAtIndex:i]];

    if( [myArray containsObject:c] )
        [result appendString:c];
}

There are better ways to do this, but this is what I think you're trying to do:

NSMutableString* result= [NSMutableString stringWithString:@""];

for( int i= 0; i < [string length]; ++i ) {

    NSString* c= [NSString stringWithFormat:@"%C", [string characterAtIndex:i]];

    if( [myArray containsObject:c] )
        [result appendString:c];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文