Add char to existing Objective string - C

I am trying to add and programmatically modify NSString on the fly. I would like to know a couple of things:

  • How do I change certain characters in a specific NSString?
  • How to add characters to a specific NSString?

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

+4
source share
4 answers

Nerver uses NSString to process strings, use NSMutableString . NSMutableString is a subclass of NSString and is used for this purpose.

From the Apple documentation:

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

The NSMutableString 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 is a more string manipulation function. See Apple documentation for NSMutableString.

Edited by:

you can 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 valid, then use the replaceCharactersInRange function on your NSMutableString to replace the character set with your string.

 - (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString 
+7
source

NSString instances are immutable. You can create new instances of NSString by adding or replacing characters with another as follows:

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

If you really need to change the instance directly, you can use NSMutableString instead of NSString.

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

+5
source

Primarily:

If you are going to change the line, you should use NSMutableString . NSStrings cannot be changed, so they have a modifiable companion.

Then NSMutableString has two methods that you find useful:

replaceCharactersInRange: withString deleteCharactersInRange:

(Sorry for not directly referencing these method links. StackOverflow, if it always imposes restrictions on me as a new user ...).

+2
source

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

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

will add "1-2-3" to str

+2
source

All Articles