How to parse a search string..

  • I have a search box, and need to parse out some special characters (' and \). The search bar text is sbar.text.. How do I interrupt the string before it is sent and remove (or warn) of these characters... ? Many thanks..

    I've tried this:

    if ([sbar.text isEqualToString:(@"'")] || [sbar.text isEqualToString:(@"\\")]) {
      NSLog(@"Found You");
      sbar.text = nil;

    (This works when the characters are first in the string..)

    and this:

    NSString *protectedString1 = sbar.text;
    NSString *protectedString2 = [protectedString1 stringByReplacingOccurrencesOfString:@"'" withString:@" "];
    sbar.text = protectedString2;

    But this loops :)
  • On 13/02/2012, at 9:02 AM, David Delmonte wrote:

    > I have a search box, and need to parse out some special characters (' and \). The search bar text is sbar.text.. How do I interrupt the string before it is sent and remove (or warn) of these characters... ? Many thanks..
    >
    > I've tried this:
    >
    > if ([sbar.text isEqualToString:(@"'")] || [sbar.text isEqualToString:(@"\\")]) {
    > NSLog(@"Found You");
    > sbar.text = nil;
    >
    > (This works when the characters are first in the string..)
    >
    > and this:
    >
    > NSString *protectedString1 = sbar.text;
    > NSString *protectedString2 = [protectedString1 stringByReplacingOccurrencesOfString:@"'" withString:@" "];
    > sbar.text = protectedString2;
    >
    > But this loops :)

    At some point presumably this string is an input to some search method. Why not just strip or modify the string there? Then it doesn't matter where the string comes from, the search method will do the right thing according to your needs.

    If you really have to annoy the user by rejecting their input as they go, there are delegate methods on the field editor which would allow you to validate the text as they type - look up "field editor" in the guides.

    --Graham
  • On 12.02.2012, at 23:02, David Delmonte wrote:
    > I have a search box, and need to parse out some special characters (' and \).

    Do you? In most cases, ignoring some of the user's input just leads to wrong result. Instead, you should determine how to correctly escape the given characters so they make it through as text instead of doing something special. E.g. for an SQLite query, you usually just have to prefix special characters with a backslash (and any backslashes you want to go through verbatim as well, so they can be told from "special character"-backslashes).

    > The search bar text is sbar.text.. How do I interrupt the string before it is sent and remove (or warn) of these characters... ? Many thanks..
    >
    > I've tried this:
    >
    > if ([sbar.text isEqualToString:(@"'")] || [sbar.text isEqualToString:(@"\\")]) {
    > NSLog(@"Found You");
    > sbar.text = nil;
    >
    > (This works when the characters are first in the string..)

    Your code above compares the entire string to another entire string. Unless the string only consists of a tick mark, or only of one backslash, this won't match. You have to use a call that searches the string for a substring, like rangeOfString:...

    > and this:
    >
    > NSString *protectedString1 = sbar.text;
    > NSString *protectedString2 = [protectedString1 stringByReplacingOccurrencesOfString:@"'" withString:@" "];
    > sbar.text = protectedString2;
    >
    > But this loops :)

    What do you mean "it loops" ? This should replace any tick marks with spaces in your string. What were you expecting it to do ... ?

    Cheers,
    -- Uli Kusterer
    "The Witnesses of TeachText are everywhere..."
  • On 13.02.2012, at 00:22, Graham Cox wrote:
    > If you really have to annoy the user by rejecting their input as they go, there are delegate methods on the field editor which would allow you to validate the text as they type - look up "field editor" in the guides.

    Isn't that what NSFormatter is for? Allow only input of numbers into a text field, and the likes? That's how NSOpenPanel does, it substitutes any colon character you type in (which is, to criminally simplify, the path component separator character on HFS) with a minus sign.

    Cheers,
    -- Uli Kusterer
    "The Witnesses of TeachText are everywhere..."
    http://www.masters-of-the-void.com
previous month february 2012 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        
Go to today