Frankrockz
Frankrockz

Reputation: 604

Deleting words from an NSString

I'm trying to remove a string from an NSString.

I have the Device Name

NSString * deviceName = [NSString stringWithFormat:@"%@", [[UIDevice currentDevice] name]];

and I'm trying to remove the 's iPhone 's iPod & 's iPad. How do I go about this? I've tried stringByReplacingOccuranceOfString but that didn't work. Any Ideas?

Upvotes: 6

Views: 1762

Answers (1)

Kenny Wyland
Kenny Wyland

Reputation: 21880

This works for me:

NSString *deviceName = @"Kenny's iPhone";
NSString *stripped = [deviceName stringByReplacingOccurrencesOfString:@"'s iPhone" withString:@""];

The stripped variable has the string @"Kenny" after that line. Remember that -stringByReplacingOccurrencesOfString: doesn't alter your existing string, it returns a new string with the changes.

Upvotes: 8

Related Questions