【问题标题】:How to find words and full stop position from NSString in iOS?如何在 iOS 中从 NSString 中查找单词和句号位置?
【发布时间】:2014-01-30 11:14:39
【问题描述】:

我想从 nsstring 中查找任何单词或句号。

NSString *str = @"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";

在此我想跟踪句号的位置,而不是第一个句号位置,每个句号位置在“Str”中。

我知道如何找到单词,但每次出现在我的 str 上时都无法获得句号位置。

这就是我在做什么

NSString *str = @"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";

NSInteger count = 0;
NSArray *arr = [str componentsSeparatedByString:@" "];
for(int i=0;i<[arr count];i++)
{
    if([[arr objectAtIndex:i] isEqualToString:@"."])
    count++;

}

我不想要这个,因为它给了我 str 字符长度而不是单词。我想在句号结束后检查一下。

if ([str3 rangeOfString:@"."].location == xOut)  {  // dont want

if ([str3 rangeOfString:@"."].location != NSNotFound)  {  // dont want

非常欢迎任何想法或建议。

【问题讨论】:

  • 请查看this链接它可能对您有帮助
  • 你真正想做什么?您是真的在寻找句号的位置还是在寻找字符串中的句子?
  • 我想检查单词后的句号位置。空间第一个句号后 str 中的 Menas 正在位置 5 上。第二个句号也是如此,依此类推...

标签: iphone objective-c nsstring uitextfield


【解决方案1】:
NSString *str = @"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";
[str enumerateSubstringsInRange:NSMakeRange(0, str.length) options:NSStringEnumerationBySentences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    [substring enumerateSubstringsInRange:NSMakeRange(0, substring.length) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        NSLog(@"%@", substring); // last word

        *stop = YES;
    }];
}];

这将为您提供每个句子的最后一个单词。

【讨论】:

  • 如何将substring的值放到nsstring中?表示 NSString *Str2 = substring;
  • __block NSString *lastWord = nil;上面的枚举然后在块中,而不是 NSLog - lastWord = substring;
【解决方案2】:

使用此代码 -

NSString *str = @"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";

NSInteger count = 0;
NSArray *arr = [str componentsSeparatedByString:@"."];

for(int i = 0; i< [arr count]; i++)
{
    NSString *sentence = [arr objectAtIndex:i];
    sentence = [sentence stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSArray *wordArray = [sentence componentsSeparatedByString:@" "];
    count = [wordArray count];
    NSLog(@"No of words after full stop is comming = %i", count);

}

【讨论】:

  • 语句报错,ARC默认不能修改快速枚举变量:声明variable_strong允许这样做
【解决方案3】:

试试这个

NSString *str = @"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";

NSInteger count = 0;
NSArray *arr = [str componentsSeparatedByString:@" "];

for(int i=0;i<[arr count];i++)
{

       NSString *arrStr=[arr objectAtIndex:i];

       for(int j=0;j<arrStr.length;j++){

       NSString *Schar=[arrStr substringWithRange:NSMakeRange(j, 1)];

       if([Schar isEqualToString:@"."])

         count++;

      }



}

这里,Count 将显示时间数。在字符串中。

【讨论】:

    【解决方案4】:

    看看@以下链接:

    NSUInteger numberOfOccurrences = [[yourString componentsSeparatedByString:@"."] count] - 1;
    

    Number of occurrences of a substring in an NSString?

    Number of Occurrences of a Character in NSString

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多