Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Replacing Strings With String

Replacing Strings With String
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 22, 2004, 04:13 PM
 
Hey guys,

Im trying to replace a string with a string, so for example, if I type in "hello", the text would be changed to "goodbye".

I did some research and found this method:

replaceOccurrencesOfString

So I tried this code in my textStorageDidProcessEditing: notification method:

Code:
NSMutableString *text = [[[docTextView string] mutableCopy] autorelease]; [text replaceOccurrencesOfString:@"hey" withString:@"bye" options:NSLiteralSearch range:NSMakeRange(0, [text length])];
I get no errors or anything, but nothing happens in the text view. I tried NSLogging the mutable string, and it seems to be fine.

Any ideas?

Thanks,
Oliver
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 22, 2004, 05:16 PM
 
Why would operating on a mutable copy of the backing store do something to the text view?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 22, 2004, 05:22 PM
 
Originally posted by Chuckit:
Why would operating on a mutable copy of the backing store do something to the text view?
Not sure what you mean, I've looked over the same type of source on mamasam, and this is how they all seem to do what I'm looking for.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 22, 2004, 06:08 PM
 
From the code you posted, what you are doing should not affect the text view at all. You first obtain the text view's string with [docTextView string]. Then you make a copy of it with mutableCopy. You then perform a search-and-replace on this new string that you just created. But that string is completely unconnected to the text view. If you wanted its contents to be displayed in the text view, you'd have to use [docTextView setString:text].
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 23, 2004, 04:27 AM
 
Originally posted by Chuckit:
From the code you posted, what you are doing should not affect the text view at all. You first obtain the text view's string with [docTextView string]. Then you make a copy of it with mutableCopy. You then perform a search-and-replace on this new string that you just created. But that string is completely unconnected to the text view. If you wanted its contents to be displayed in the text view, you'd have to use [docTextView setString:text].
Or if it's really a NSTextView (sounds like it's more likely to be a NSTextField) you could operate directly on its -textStorage (NSTextStorage inherits from NSMutableAttributedString).
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 23, 2004, 06:50 AM
 
setString: did it, thanks. I got a little confused .

Now I have another little problem, If I try this code:

[text replaceOccurrencesOfString:@"£" withString:@"£" options:NSLiteralSearch range:NSMakeRange(0, [text length])];

If I type in £, nothing happens, but if I replace the £ with a word, it works perfectly. I guess this means something to do with encoding. I am using Unicode (UTF-8) and I don't see why it shouldn't work.

Thanks,
Oliver
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 23, 2004, 07:53 AM
 
Constant strings don't work too well with non-ASCII characters. You'd have to either escape it as a unicode character or load it from a strings file.
     
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status: Offline
Reply With Quote
May 23, 2004, 11:58 PM
 
not to mention your source files can become corrupted if they have a bunch of non-ASCII characters in them (well, it happened to me at least; the file wasn't lost, but my "©" mysteriously changed to something else when I wasn't looking)
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 24, 2004, 12:01 AM
 
Originally posted by Angus_D:
Or if it's really a NSTextView (sounds like it's more likely to be a NSTextField) you could operate directly on its -textStorage (NSTextStorage inherits from NSMutableAttributedString).
NSMutableAttributedString, however, does not inherit from NSMutableString and lacks many methods that the latter has (including the one Oliver is using).

Also, since NSTextField is just an NSControl wrapper for an NSTextView, any techniques applicable to one ought to be applicable to the other.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:32 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2