Skip to content

Instantly share code, notes, and snippets.

@bricklife
Created August 16, 2012 12:33
Show Gist options
  • Save bricklife/3369833 to your computer and use it in GitHub Desktop.
Save bricklife/3369833 to your computer and use it in GitHub Desktop.
removing characters
// making a string
NSMutableString *s = [[NSMutableString alloc] initWithCapacity:256];
for (char c = 0x20; c < 0x7f; c++) {
[s appendFormat:@"%c", c];
}
[s appendString:@"日本語"];
NSString *original = [NSString stringWithString:s];
NSLog(@"original:[%@]", original);
// setting a character set
NSMutableCharacterSet *workingSet = [[NSCharacterSet alphanumericCharacterSet] mutableCopy];
[workingSet addCharactersInString:@"-._"];
// removing characters
NSMutableString *output = [[NSMutableString alloc] initWithCapacity:256];
NSRange range = NSMakeRange(0, original.length);
while (true) {
NSRange r = [original rangeOfCharacterFromSet:workingSet options:NSCaseInsensitiveSearch range:range];
if (r.location == NSNotFound) {
break;
}
[output appendString:[original substringWithRange:r]];
range.location = r.location + r.length;
range.length = original.length - range.location;
}
NSLog(@"output: [%@]", output);
// NSLog
// original:[ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~日本語]
// output: [-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz日本語] <= why?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment