-1

I want to find any word or full stop from a 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.";

In this I want to track the position of full stop not first full stop positions every full stop position in "Str".

I know how to find words but not able to get full stop position every time it come on my str.

Here is what am I doing

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

}

I don't want this as it give me str character length not by word. I want to check after how many words full stop is comming.

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

or

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

Any Idea or suggestion would be highly welcome.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Tina
  • 31
  • 1
  • 5
  • Please check [this](http://stackoverflow.com/questions/3632672/nsstring-character-position) link it might help you – Saurav Mac Jan 30 '14 at 11:40
  • What do you actually want to do? Are you really looking for the positions of full stops or are you looking for sentences in the string? – Fogmeister Jan 30 '14 at 12:12
  • I want to check full stop position after words. Menas in str after space first full stop is comming on position 5. And same for second full stop and so on... – Tina Jan 30 '14 at 12:40

4 Answers4

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;
    }];
}];

This will give you the last word of each sentence.

Andrew
  • 3,166
  • 21
  • 32
  • how to put the value of substring to nsstring? means NSString *Str2 = substring; – Tina Jan 30 '14 at 11:41
  • __block NSString *lastWord = nil; above the enumeration then in the block, instead of the NSLog - lastWord = substring; – Andrew Jan 30 '14 at 12:16
1

Use this code -

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

}
Akshay Nalawade
  • 1,447
  • 2
  • 14
  • 29
  • sentance is giving error, fast enumeration variable cant be modified in ARC by default: declare the variable_strong to allow this – Tina Jan 30 '14 at 11:53
1

Try this

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

      }



}

here,Count will show number of time . is in string.

Akshay Nalawade
  • 1,447
  • 2
  • 14
  • 29
0

Have a look @ following links :

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

Number of occurrences of a substring in an NSString?

Number of Occurrences of a Character in NSString

Hope this helps :)

Community
  • 1
  • 1
Swati
  • 2,870
  • 7
  • 45
  • 87