• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Simple string replacement
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple string replacement


  • Subject: Re: Simple string replacement
  • From: "Eric Hochmeister" <email@hidden>
  • Date: Thu, 23 Feb 2006 11:39:23 -0500

Hi Stephen,

> tagName = (NSMutableString*)[tagName replaceOccurrencesOfString:@"a"
> withString:@"_" options:NSLiteralSearch range:NSMakeRange(0, [tagName
> length])];

Its doing the right thing, you're just reassigning the value of tagName.

You send tagName the message
"replaceOccurrencesOfString:withString:options:range:" which performs
actions on tagName.  If you look at the definition of
"replaceOccurrencesOfString:withString:options:range:" you'll see it
returns an unsigned int.

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

So effectively above, you perform the operation on tagName.  It
replaces the string with the desired string, and once that's done,
you're changing the value of tagName to be equal to the return value
of "replaceOccurrencesOfString:withString:options:range:".

So just don't assign tagName to the return value and you're fine.

ie.

[tagName replaceOccurrencesOfString:@"a" withString:@"_"
options:NSLiteralSearch range:NSMakeRange(0, [tagName length])];

Hope that helps,

Eric


On 2/23/06, Stephen Caudill <email@hidden> wrote:
> A no-brainer for a lot of you, I'm sure, but this has me stumped.  I
> have a variable string, that's going to come in the form of: "foo
> bar", "foobar" or "foo bar baz" with which I'd like to do a simple
> replacement on the space with an underscore, if there's a space
> present... I'm attempting this:
>
> NSMutableString *tagName = [f lowercaseString];
> tagName = (NSMutableString*)[tagName replaceOccurrencesOfString:@"a"
> withString:@"_" options:NSLiteralSearch range:NSMakeRange(0, [tagName
> length])];
>
> Which is yielding nil for everything that passes through it...  Can
> someone point me to where I'm going wrong?
>
> Thanks,
> Stephen
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Simple string replacement (From: Stephen Caudill <email@hidden>)

  • Prev by Date: Re: Simple string replacement
  • Next by Date: Re: Simple string replacement
  • Previous by thread: Re: Simple string replacement
  • Next by thread: Re: Simple string replacement
  • Index(es):
    • Date
    • Thread