[ACCEPTED]-Replacing one character in a string in Objective-C-nsstring

Accepted answer
Score: 93

If it is always the same character you can 4 use:

stringByReplacingOccurrencesOfString:withString:

If it is the same string in the same 3 location you can use:

stringByReplacingOccurrencesOfString:withString:options:range:

If is just a specific 2 location you can use:

stringByReplacingCharactersInRange:withString:

Documentation here: https://developer.apple.com/documentation/foundation/nsstring

So 1 for example:

NSString *someText = @"Goat";
NSRange range = NSMakeRange(0,1);
NSString *newText = [someText stringByReplacingCharactersInRange:range withString:@"B"];

newText would equal "Boat"

Score: 33
NSString *str = @"123*abc";
str = [str stringByReplacingOccurrencesOfString:@"*" withString:@""];
//str now 123abc

0

Score: 7

Here is the code:

[aString stringByReplacingCharactersInRange:NSMakeRange(3,1) withString:@"B"];

0

Score: 5

Use the replaceCharactersInRange: withString: message on a NSMutableString object.

0

More Related questions