Professional Web Applications Themes

Replacing strings with strings? - Mac Programming

NOTE: Beginner to Cocoa (be gentle) :) In a .txt file - I'd like to have some text: "My name is %name% and I live in %town%." After importing this into my app, I'd like to be able to replace the strings %name% and %town% with other strings; "Dave" and "Hornchurch" Is there a function that does this in Cocoa? TIA Dave....

  1. #1

    Default Replacing strings with strings?

    NOTE: Beginner to Cocoa (be gentle) :)

    In a .txt file - I'd like to have some text: "My name is %name% and I
    live in %town%."

    After importing this into my app, I'd like to be able to replace the
    strings %name% and %town% with other strings; "Dave" and "Hornchurch"

    Is there a function that does this in Cocoa?

    TIA

    Dave.


    Dave Guest

  2. #2

    Default Re: Replacing strings with strings?

    In article <2004022416375316807%davewintercommanderbondnet >,
    Dave Winter <net> wrote:
     

    The basic strategy when searching for something like this is to open the
    header file for the appropriate class (NSString in this case) or use a
    program like CocoaBrowser, then have a look at all of the methods
    provided by that class.

    In this case, you can use the following NSMutableString method:

    - (unsigned int)replaceOccurrencesOfString:(NSString *)target
    withString:(NSString *)replacement options:(unsigned)opts
    range:(NSRange)searchRange;
    Michael Guest

  3. #3

    Default Re: Replacing strings with strings?

    On Tue, 24 Feb 2004, Dave Winter wrote:
     
    You probably want something like
    [NSString stringWithFormat:"My name is %, my town is %",name,town];

    where name and town are nsstrings with the appropriate contents. This uses
    the same % escapes as printf does, with the addition of % which prints
    objects.

    Fred

    Frederick Guest

  4. #4

    Default Re: Replacing strings with strings?

    Michael Ash <com> wrote:
     
    >
    > The basic strategy when searching for something like this is to open the
    > header file for the appropriate class (NSString in this case) or use a
    > program like CocoaBrowser, then have a look at all of the methods
    > provided by that class.
    >
    > In this case, you can use the following NSMutableString method:
    >
    > - (unsigned int)replaceOccurrencesOfString:(NSString *)target
    > withString:(NSString *)replacement options:(unsigned)opts
    > range:(NSRange)searchRange;[/ref]

    This will work if you know in advance a limited set of expressions, e.g.
    %name% and %town% and some others. But if you want to find *all*
    percent-bounded words no matter what word they may be, you will need to
    use an NSScanner or even just hand the whole thing over to Perl for
    parsing. m.


    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

Similar Threads

  1. Extracting strings delimited by other strings
    By Scott Bass in forum PERL Modules
    Replies: 2
    Last Post: May 7th, 02:26 AM
  2. Replacing # character in user input strings
    By mate of the state in forum Coldfusion - Advanced Techniques
    Replies: 2
    Last Post: April 26th, 01:55 PM
  3. replacing certain words in strings
    By Martin Brunner in forum PHP Development
    Replies: 5
    Last Post: August 6th, 12:50 AM
  4. replacing everything between 2 strings
    By Matt Palermo in forum PHP Development
    Replies: 1
    Last Post: July 22nd, 01:48 AM
  5. [PHP] replacing everything between 2 strings
    By Matt Palermo in forum PHP Development
    Replies: 0
    Last Post: July 22nd, 12:16 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139