如何在iOS中从NSString中找到单词和句号位置?

时间:2014-01-30 11:14:40

标签: iphone objective-c nsstring uitextfield

我想从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++;

}

我不想要这个,因为它不是用字来给我字符长度。我想检查完全停止的字数是多少。

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

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

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

4 个答案:

答案 0 :(得分: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;
    }];
}];

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

答案 1 :(得分: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.";

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);

}

答案 2 :(得分: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.";

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将显示时间。在字符串中。

答案 3 :(得分:0)

看看@以下链接:

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

Number of occurrences of a substring in an NSString?

Number of Occurrences of a Character in NSString

希望这会有所帮助:)