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 characters in a string?

Replacing characters in a string?
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 4, 2002, 10:18 PM
 
Is there an easy way to replace characters in a string? Perhaps an NSString or NSScanner method? I checked the docs, but couldn't find anything obvious. Specifically, I want to replace all of the spaces and commas with plus signs.

Thanks,
kman
     
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Apr 4, 2002, 11:59 PM
 
Look into these two methods:

-[NSString rangeOfString: options: range:]

-[NSMutableString replaceCharactersInRange: withString:]

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Apr 6, 2002, 01:59 AM
 
The easy way would be:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
aString = [[aString componentsSeparatedByString:<font color = orange>@",<font color = red>"</font>] componentsJoinedByString:<font color = orange>@"</font>+"</font>];
</font>[/code]
and again for " ", etc. If the code is getting run a ton of times, you might want to check with -rangeOfString: that it's actually in there before going to the above work.

You might make it more efficient by creating an NSCharacterSet with a comma and space, and using -rangeOfCharaterInSet:options:range: in a loop.

You could use NSScanner as well -- just make sure to set the skip chararacter set to nil else it may skip over your spaces.

[ 04-06-2002: Message edited by: lindberg ]
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 6, 2002, 09:15 AM
 
Thanks for the good suggestions. I ended up using an NSScanner in a while loop. I used scanUpToCharactersInSet:commaAndSpaceSet to scan the string up to the comma or space into a temp string and then used stringByAppendingString:@"+" to add the plus sign. I then scanned the comma or space into NULL and repeated until the scanner was done. Works like a charm!

Now on to getting the text in certain rows of an NSTableView to be red. I've tried NSAttributedString and addAttribute value: [NSColor redColor], but I keep getting an NSColor not defined error kman

thanks,

[ 04-06-2002: Message edited by: kman42 ]
     
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Apr 7, 2002, 04:38 PM
 
don't think so complicated... ;-)

in your tableviews's delegate, implement

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (void)tableView NSTableView *)aTableView willDisplayCell id)aCell forTableColumn NSTableColumn *)aTableColumn row int)rowIndex
</font>[/code]

and then find out whether you want to display that column/cell in red or not:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
if (someCondition)
{
[aCell setTextColor:[NSColor redColor]];
} else {
[aCell setTextColor:[NSColor blackColor]];
}
</font>[/code]

hope that helps.
     
   
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 12:33 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