287

How to replace a character is a string in Objective-C?

Omar Faroque Anik
  • 2,531
  • 1
  • 29
  • 42
4thSpace
  • 43,672
  • 97
  • 296
  • 475

6 Answers6

806

You could use the method

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target 
                                        withString:(NSString *)replacement

...to get a new string with a substring replaced (See NSString documentation for others)

Example use

NSString *str = @"This is a string";

str = [str stringByReplacingOccurrencesOfString:@"string"
                                     withString:@"duck"];
epatel
  • 45,805
  • 17
  • 110
  • 144
  • 2
    I thought the point of having NSString and an NSMutableString subclass was because an instance of NSString is *unchangeable*. While--like any sane person, I'd rather have ducks than strings any day--the fact that you just overwrote the contents of `str` just blew my mind. – ele Apr 01 '13 at 21:42
  • 1
    But I guess the adress of `str` changed in the process – Colas Apr 02 '13 at 18:49
  • 16
    it doesn't change. str now contains a whole new string. stringByReplacingOccurencesOfString does NOT mutate the string. It simply return a new string. – Septiadi Agus Jun 28 '13 at 08:56
22

NSString objects are immutable (they can't be changed), but there is a mutable subclass, NSMutableString, that gives you several methods for replacing characters within a string. It's probably your best bet.

mipadi
  • 398,885
  • 90
  • 523
  • 479
10

If you want multiple string replacement:

NSString *s = @"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"%@", s); // => foobarbazfoo
approxiblue
  • 6,982
  • 16
  • 51
  • 59
tania_S
  • 1,350
  • 14
  • 23
8

It also posible string replacement with stringByReplacingCharactersInRange:withString:

for (int i = 0; i < card.length - 4; i++) {

    if (![[card substringWithRange:NSMakeRange(i, 1)] isEqual:@" "]) {

        NSRange range = NSMakeRange(i, 1);
        card = [card stringByReplacingCharactersInRange:range withString:@"*"];

    }

} //out: **** **** **** 1234
Marlon Ruiz
  • 1,774
  • 16
  • 14
0
NSString *stringreplace=[yourString stringByReplacingOccurrencesOfString:@"search" withString:@"new_string"];
alfonoso
  • 3
  • 2
Shaik Tamim
  • 95
  • 12
  • Please use the [edit] link to explain how this code works and don't just give the code, as an explanation is more likely to help future readers. See also [answer]. [source](http://stackoverflow.com/users/5244995) – Jed Fox Jun 09 '17 at 15:00
0

The problem exists in old versions on the iOS. in the latest, the right-to-left works well. What I did, is as follows:

first I check the iOS version:

if (![self compareCurVersionTo:4 minor:3 point:0])

Than:

// set RTL on the start on each line (except the first)  
myUITextView.text = [myUITextView.text stringByReplacingOccurrencesOfString:@"\n"
                                                           withString:@"\u202B\n"];
Meir
  • 994
  • 12
  • 25