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.
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.
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);
}
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.
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 :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.