Re: String Character Replacement
Subject : Re: String Character Replacement
From: "stephen joseph butler" <email@hidden >
Date: Fri, 10 Nov 2006 19:56:52 -0600
Delivered-to: email@hidden
Delivered-to: email@hidden
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=K8gbFLFabUdBWdB8pvzmF/7qU+d17rDeFQi5lJoxZEA8g+lRGjSK0dVy+Frwi/oFGYvnlroQIVaDfcYbL0GZIbnUHfTLiNehnDtBICaQTe2EsBUE+FbLBvesOdPI4LS4ORlFrdu1wOfvVKgD7QK/SkMaBNtXz9XDbGAOZUrcGgs=
2006/11/10, email@hidden <email@hidden>:
What would be the most efficient way to replace characters in a string? Say I
have an NSString/NSMutableString that contains...
Joe:Smith:MI:123:456:8967:M
..and I want to replace all the colons with tabs?
Myself, I'd probably use NSScanner. But this is such a simple task,
I'm tempted to recommend this method:
NSString* doReplace( NSString *source, unichar from, unichar to )
{
unichar *temp = NULL;
unsigned int tempLength = 0;
unsigned int i;
if (source == nil)
return nil;
tempLength = [source length];
temp = (unichar*)malloc( sizeof( unichar ) * tempLength );
if (temp == NULL)
return nil;
[source getCharacters:temp];
for (i = 0; i < tempLength; ++i)
{
if (temp[ i ] == from)
temp[ i ] = to;
}
return [[[NSString alloc] initWithCharactersNoCopy:temp
length:tempLength freeWhenDone:YES] autorelease];
}
You can't use this outside the Basic Multilingual Plane, but for what
you're doing it is fine.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden
This email sent to email@hidden
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE
Contact Apple | Terms of Use | Privacy Policy
Copyright © 2007 Apple Inc. All rights reserved.