Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 956
Frankrockz Avatar asked Feb 05 '11 00:02

Frankrockz


1 Answers

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.

like image 61
Kenny Wyland Avatar answered Sep 28 '22 07:09

Kenny Wyland