# Search and replace string



## kainjow (May 12, 2002)

Does anybody know how to replace certain strings withint another string? Like...

NSString *alphabet = @"abcdefghijklmnopqrstuvwxyz";

I want to replace "abc" in the alphabet with "cba". Does anybody know how to do that? Just with normal strings, not with attributed strings.

Thanks!!


----------



## kainjow (May 14, 2002)

So you guys don't know how to do a basic function in Cocoa?

I thought you guys were smart here! Prove it to me!


----------



## nkuvu (May 14, 2002)

> _Originally posted by kainjow _
> *I thought you guys were smart here! Prove it to me! *


Hmm...  I suddenly got a flicker on my Troll-O-Meter...

A quick search on Google turned up this link at the MacDevcenter.

I don't know Cocoa, but I do know how to use the web...


----------



## kainjow (May 15, 2002)

Yes I did use Google and I know how to use the web and I've read that site you referred me to and no it doesn't answer my question. Thanks anyways...Anyone else?


----------



## zots (May 15, 2002)

its in page 2 of the above mentioned article or here:

http://developer.apple.com/techpubs/macosx/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSMutableString.html 

```
NSMutableString *alphabet = [[NSMutableString alloc] initWithString:@"abcdefghijklmnopqrstuvwxyz"];
[alphabet replaceCharactersInRange:NSMakeRange(0,2) withString:@"cba"];
```


----------



## nkuvu (May 15, 2002)

Yeah, that seemed to me to be the answer.  Any reasons this doesn't work, kainjow?


----------



## iconara (May 15, 2002)

hi boys and girls:


s/abc/bca/g



of course that won't work in objc... maybe with the omniframeworks installed...

i suggest the NSRange way described above, it's probably the easiest way. had it been perl though... s/abc/cba/ !


theo


----------



## nkuvu (May 15, 2002)

Um, yeah.  If this was Perl, I'd also have about twenty pages to send for development information.


----------



## kainjow (May 17, 2002)

Thanks a lot!! That works for me and helps so much. Where did you find that? It seems like I looked all over the documentation and couldn't find it!

Thanks!


----------



## nkuvu (May 17, 2002)

Assuming you're referring to the info from zots, it's in the documentation I linked to in my first post.  Or in the documetation linked from that post (to the Apple developer docs).

If you're talking about the s/abc/cba/ bit, that's general Perl, so I don't know where iconara found it...


----------

