How to find a particular text in UITextField?

2019-07-25 08:48发布

问题:

In my iPhone app I have a UITextField ans text field value is being taken into an NSString, I want to find if a particular character " is typed in that

How can I do that?

UITextField *textField;
NSString *String=textField.text;

-(IBAction)FindCharecter:(id)sender
{
    // if String Contain ",then i wish to print @"Found symbol"
}

回答1:

 UITextField *textField;
 NSString *string=textField.text;
 if ([string rangeOfString:@"\""].location == NSNotFound) {
   NSLog(@"string does not \"");
  } else {
   NSLog(@"string contains \"");
 }   

check this



回答2:

Try This:

-(IBAction)FindCharecter:(id)sender
  {

    // if String Contain ",then i wish to print @"Found symbol"
     if ([String rangeOfString:@"\""].location != NSNotFound) {

          NSLog(@"Found");
     }
     else{

          NSLog(@"Not Found");
      }

  }


回答3:

Implement the following delegate of UITextField

-(NSString *)ValidateSearchString:(NSString *) aString{   

    NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"YourString"] invertedSet];

    if ([aString rangeOfCharacterFromSet:set].location != NSNotFound) { 
        return @"Not Found";
    }

    return @"Found";;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSLog("particular character  %@",[self ValidateSearchString:string]);

     // string will contain the particular character typed;

    return YES;
}


回答4:

You need to confirm and implement UITextFieldDelegate protocol.

1) If you want to check for a pattern on the go (when user typing itself), you need to implement

- (BOOL)textField:(UITextField *)textField 
    shouldChangeCharactersInRange:(NSRange)range 
     replacementString:(NSString *)string

2) If you want to check when user completed typing, you may want to implement

- (void)textFieldDidEndEditing:(UITextField *)textField

Now to do the actual comparison process

if([textFieldString rangeOfString:@"YOUR_COMPARISON_STRING"].location == NSNotFound){
     //your pattern is not typed yet
}else{
    //There it is..
}


回答5:

You can use shouldChangeCharactersInRange delegate method for this.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if([string isEqualToString:@"\""])
   {
     NSLog(@"Entered \" ");
   }
}

Or you can use: rangeOfString of NSString class.

if ([textField.text rangeOfString:@"\""].location == NSNotFound)
 {
    NSLog(@"Entered \" ");
 }

For reference:

textField:shouldChangeCharactersInRange:replacementString:

Asks the delegate if the specified text should be changed.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

Parameters

textField

The text field containing the text.

range

The range of characters to be replaced

string

The replacement string. 

Return Value

YES if the specified text range should be replaced; otherwise, NO to keep the old text. Discussion

The text field calls this method whenever the user types a new character in the text field or deletes an existing character. Availability

Available in iOS 2.0 and later.

UITextFieldDelegate

rangeOfString:

Finds and returns the range of the first occurrence of a given string within the receiver. - (NSRange)rangeOfString:(NSString *)aString

Parameters

aString

The string to search for. This value must not be nil.

Important: Raises an NSInvalidArgumentException if aString is nil.

Return Value

An NSRange structure giving the location and length in the receiver of the first occurrence of aString. Returns {NSNotFound, 0} if aString is not found or is empty (@"").

Discussion

Invokes rangeOfString:options: with no options.

This method detects all invalid ranges (including those with negative lengths). For applications linked against OS X v10.6 and later, this error causes an exception; for applications linked against earlier releases, this error causes a warning, which is displayed just once per application execution. Availability

Declared In NSString.h Declared In UITextField.h

NSString Class