Want to get index of the last occurrence of # but bellow code working fine for the other character but not giving perfect result for particularity "#"
Code Working fine in viewDidLoad but not working in textfield shouldChangeCharactersInRange.
Code:
txtTest.text = @"@ashish @test #vijay $4030 @post";
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSRange range = [textField.text rangeOfString:@"a" options:NSBackwardsSearch];
NSLog(@"a :: %d ",range.location);
range = [str rangeOfString:@"@" options:NSBackwardsSearch];
NSLog(@"@ :: %d ",range.location);
range = [str rangeOfString:@"#" options:NSBackwardsSearch];
NSLog(@"# :: %d",range.location);
range = [str rangeOfString:@"$" options:NSBackwardsSearch];
NSLog(@"$ :: %d\n",range.location);
}
Result :: a :: 17 @ :: 26 # :: 2147483647 $ :: 20
Try %lu unsigned long instead of %d . Its working fine
NSString *str = @"ashish @test #vijay $4030 @post";
NSRange range = [str rangeOfString:@"a" options:NSBackwardsSearch];
NSLog(@"a :: %lu ",(unsigned long)range.location);
range = [str rangeOfString:@"@" options:NSBackwardsSearch];
NSLog(@"@ :: %lu ",(unsigned long)range.location);
range = [str rangeOfString:@"#" options:NSBackwardsSearch];
NSLog(@"# :: %lu",(unsigned long)range.location);
range = [str rangeOfString:@"$" options:NSBackwardsSearch];
NSLog(@"$ :: %lu\n",(unsigned long)range.location);
a :: 17
@ :: 26
# :: 13
$ :: 20
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With