1

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"
}
4

5 に答える 5

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

check this

于 2013-01-01T08:32:37.897 に答える
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");
      }

  }
于 2013-01-01T08:34:11.200 に答える
0

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;
}
于 2013-01-01T08:32:52.793 に答える
0

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

于 2013-01-01T08:34:02.667 に答える
0

UITextFieldDelegate プロトコルを確認して実装する必要があります。

1)外出先で(ユーザーが自分自身を入力するときに)パターンをチェックしたい場合は、実装する必要があります

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

2)ユーザーがいつ入力を完了したかを確認したい場合は、実装することをお勧めします

- (void)textFieldDidEndEditing:(UITextField *)textField

次に、実際の比較プロセスを実行します

if([textFieldString rangeOfString:@"YOUR_COMPARISON_STRING"].location == NSNotFound){
     //your pattern is not typed yet
}else{
    //There it is..
}
于 2013-01-01T08:35:48.860 に答える