Creating and Initializing a Mutable String

+ stringWithCapacity:

Returns an empty NSMutableString object with initial storage for a given number of characters.

Example

NSMutableString *mutableString = [NSMutableString stringWithCapacity:5];
NSLog(@"Empty:%@",mutableString);
		

Output

2014-04-12 20:43:57.481 iOS-Tutorial[2465:a0b] Empty:
		

- initWithCapacity:

Returns an NSMutableString object initialized with initial storage for a given number of characters.

Example

NSMutableString *mutableString = [[NSMutableString alloc ]initWithCapacity:5];
NSLog(@"Empty:%@",mutableString);
		

Output

2014-04-12 20:43:57.481 iOS-Tutorial[2465:a0b] Empty:
		

Modifying a String

- appendFormat:

Adds a constructed string to the receiver.

Example

NSMutableString *mutableString = [[NSMutableString alloc ]initWithCapacity:5];
[mutableString appendFormat:@"%d %@",1,@"Eezy"];
NSLog(@"%@",mutableString);
		

Output

2014-04-12 20:46:29.071 iOS-Tutorial[2488:a0b] 1 Eezy
		

- appendString:

Adds to the end of the receiver the characters of a given string.

Example

NSMutableString *mutableString = [[NSMutableString alloc ]initWithCapacity:5];
[mutableString appendString:@"Eezy"];
NSLog(@"%@",mutableString);
		

Output

2014-04-12 20:47:30.060 iOS-Tutorial[2501:a0b] Eezy
		

- deleteCharactersInRange:

Removes from the receiver the characters in a given range.

Example

NSMutableString *mutableString = [[NSMutableString alloc ]initWithCapacity:5];
[mutableString appendString:@"Eezy"];
[mutableString deleteCharactersInRange:NSMakeRange(2,2)];
NSLog(@"%@",mutableString);
		

Output

2014-04-12 20:48:54.517 iOS-Tutorial[2513:a0b] Ee
		

- insertString:atIndex:

Inserts into the receiver the characters of a given string at a given location.

Example

NSMutableString *mutableString = [[NSMutableString alloc ]initWithCapacity:5];
[mutableString appendString:@"Eezy"];
[mutableString insertString:@"iOS " atIndex:0];
NSLog(@"%@",mutableString);
		

Output

2014-04-12 20:49:48.307 iOS-Tutorial[2528:a0b] iOS Eezy
		

- replaceCharactersInRange:withString:

Replaces the characters from aRange with those in aString.

Example

NSMutableString *mutableString = [[NSMutableString alloc ]initWithCapacity:5];
[mutableString appendString:@"Eezy"];
[mutableString replaceCharactersInRange:NSMakeRange(0, 4) withString:@"Tutorial"];
NSLog(@"%@",mutableString);
		

Output

2014-04-12 20:50:37.118 iOS-Tutorial[2547:a0b] Tutorial
		

- replaceOccurrencesOfString: withString: options: range:

Replaces all occurrences of a given string in a given range with another given string, returning the number of replacements.

Example

NSMutableString *mutableString = [[NSMutableString alloc ]initWithCapacity:5];
[mutableString appendString:@"Eezy"];
[mutableString replaceOccurrencesOfString:@"E" withString:@"È" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 3)];
NSLog(@"%@",mutableString);		

Output

			// Implementation in progress
		

- setString:

Replaces the characters of the receiver with those in a given string.

Example

NSMutableString *mutableString = [[NSMutableString alloc ]initWithCapacity:5];
[mutableString setString:@"Eezy"];
NSLog(@"%@",mutableString);
		

Output

2014-04-12 20:54:16.490 iOS-Tutorial[2586:a0b] Eezy
		

Advertisements