4

I'm trying to append and programmatically modify a NSString on the fly. I'd like to know a couple of things:

  • How do I modify specific chars in a defined NSString?
  • How do I add chars in a defined NSString?

For example if I have the following defined: NSString *word = [[NSString alloc] initWithString:@"Hello"]; how would I be able to replace the letter "e" with "a" and also how would I add another char to the string itself?

4 Answers 4

7

Nerver use NSString for string manipulation,Use NSMutableString. NSMutableString is the subclass of NSString and used for that purpose.

From Apple Documentation:

The NSString class declares the programmatic interface for an object that manages immutable strings. (An immutable string is a text string that is defined when it is created and subsequently cannot be changed. NSString is implemented to represent an array of Unicode characters (in other words, a text string).

The mutable subclass of NSString is NSMutableString.

NSMutableString *word = [[NSMutableString alloc] initWithString:@"Hello"];
//Replace a character 
NSString*  word2 = [word stringByReplacingOccurrencesOfString:@"e" withString:@"a"];

[word release];
word = nil ;

word = [[NSMutableString alloc] initWithString:word2 ];
//Append a Character
[word appendString:@"a"];

There are more string manipulating function See Apple Documentation for NSMutableString

Edited:

you could first use rangeOfString to get the range of the string (in your case @"e").

- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask

then check the NSRange object if it's valid then use the replaceCharactersInRange function on your NSMutableString to replace the set of characters with your string.

- (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString
Sign up to request clarification or add additional context in comments.

3 Comments

You are not actually modifying the NSMutableString and I think you are leaking memory in your example. Even though word is initially an instance of NSMutableString, stringByReplacingOccurrencesOfString:withString is a method on the NSString superclass, which creates and returns a new autoreleased, immutable NSString instance.
again, i think the issue here is if you have two of the same letter in your string like "hello" with "l" you will run into problems if you only want to replace one of the two "l"s.
Good answer overall but, as a nit, NSMutableString is not needed in your example, as you never modify any of the string variables. replaceOccurances:~ and stringByAppending~: et al generate new NSStrings -- no mutating needed! :) (Ah, I see you need a mutable to use your appendString: example at the end.)
5

NSString instances are immutable. You can create new NSString instances by appending or replacing characters in another like this:

NSString *foo = @"Foo";
NSString *bar = @"Bar";

NSString *foobar = [foo stringByAppendingString:bar];
NSString *baz = [bar stringByReplacingOccurrencesOfString:@"r" withString:@"z"];

If you really need to modify an instance directly, you can use an NSMutableString instead of an NSString.

If you really want to use primitive characters, NSString has a couple of initializers that can take character arrays (e.g. initWithCharacters:length:).

3 Comments

Thanks for the help, can you connect a UILabel to a NSMutableString? Also the problem with your replacement method is that words like "Hello" have chars that are repeated ("l" in "Hello" is an example of this). If you use your method, you would have to replace each "l".
It doesn't appear that way. Even though it's a little ugly, I think you need to do something like [label setText:[[label text] stringByAppendingString:@"newSuffix"]];.
To answer your question about replacing repeated letters, you can use stringByReplacingCharactersInRange:withString: to be more specific about what you're replacing with what.
2

First things first:

If you are going to modify an string, you have to use NSMutableString. NSStrings can't be modified, hence they have a modifiable companion.

Then, NSMutableString has two methods that you are going to find helpful:

replaceCharactersInRange:withString deleteCharactersInRange:

(Sorry for not linking directly to those method's links. StackOverflow if always imposing limitations to me as a new user...).

Comments

2

Just to add, NSMutableString has a method called appendFormat, which can be of great help when appending stuff:

[str appendFormat:@"%@-%@-%@", @"1", @"2",@"3"]

will append "1-2-3" to to str

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.