Deleting Characters from NSString

Discussion in 'iOS Programming' started by HarryWorksInc, Apr 24, 2010.

  1. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #1
    I am currently working on an application that contacts a mySQL data base using php. In my app there is a string that will be submitted to the server but obviously if the string has a space in it the app cannot load the URL. Does anyone know how to replace spaces in a URL to %20?
     
  2. kaydell.leavitt macrumors regular

    Joined:
    Apr 19, 2010
    #2
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Look up

    Code:
    - (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc;
     
  4. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010
  5. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010
    #5
    Ok, sorry i've been looking over the NSStringEncoding and if I am understanding the Class References correctly there is no string encoder for a URL or for replacing " " with "%20". Can you give an example or just the encoder?
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    See the post two above this one: there is not an encoding (as this is not a character encoding in that sense of the word). But there is a method to escape those characters.
     
  7. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    Use NSUTF8StringEncoding for the encoding parameter with this method.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Assuming the server is able to handle that encoding. Otherwise, you may want to try something more compatible.
     
  10. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010
    #10
    One last thing I,v been incorporated the code
    HTML:
    [URL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    but it doesn't seem to be working. When i return the url the spaces are still spaces.
     
  11. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    Post your code.

    Anyway I can guess what you are doing:

    Code:
    NSString *URL = @"my url with spaces";
    [URL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSLog(@"%@",URL); // Logs my url with spaces
    
    But, as we all know, NSString is immutable. And the method returns an NSString. So the obvious usage is:
    Code:
    NSString *URL = @"my url with spaces";
    URL = [URL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSLog(@"%@",URL); // Logs my%20url%20with%20spaces
    
     
  12. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010
    #12
    Oh, thanks I was still using an NSMutableString but once i changed it back to an NSString and it worked.
     
  13. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #13
    I can see how this could be confusing but using a NSMutableString does not alter the signature or semantics of the method. If NSMutableString provided a method that did the same thing in-place it would not return a NSString object...
     

Share This Page