2

Pretty straightforward question:

Is there an Objective-C equivalent that iOS developers can use with NSString objects to do the same thing as Java's replaceAll("\p{Cntrl}", "") call on a String?

We already have this in Java:

String noControlCharsString = maybeHasControlCharsString.replaceAll("\\p{Cntrl}", "");

I want something like this in Objective-C too:

NSString *noControlCharsString = [maybeHasControlCharsString stringByReplacingControlCharsWithString:@""];

Thanks in advance.

AG3

Art Geigel
  • 1,915
  • 3
  • 22
  • 24

3 Answers3

2

Try to use this one.. This is used to replace character.

  NSString *str = @"\\p{Cntrl}AB/bar:baz\\p{Cntrl}foo";
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\\p{Cntrl}"];
    str = [[str componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
    NSLog(@"%@", str);

Other wise This is used to replace whole string.

   NSString *str = @"\\p{Cntrl} This is a string \\p{Cntrl}";

    str = [str stringByReplacingOccurrencesOfString:@"\\p{Cntrl}"
                                             withString:@""];

        NSLog(@"%@", str);
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • 1
    Thanks but the "\\p{Cntrl}" used in Java is a regex that actually specially matches all control characters that may appear in the string -- not just the actual textual letters making up that special sequence. I'm looking for a solution that rips out _all_ those control characters. – Art Geigel May 28 '13 at 06:44
  • What you wanna replace either one by one character or entire string?. – Dharmbir Singh May 28 '13 at 07:07
2

The stringByReplacingOccurrencesOfString method of NSString has a NSRegularExpressionSearch option that accepts the same pattern \p{Cntrl} as your Java method:

NSString *maybeHasControlCharsString = @"\nabc\r\ndef\r";
NSString *noControlCharsString =
    [maybeHasControlCharsString stringByReplacingOccurrencesOfString:@"\\p{Cntrl}"
                                       withString:@""
                                          options:NSRegularExpressionSearch
                                            range:NSMakeRange(0, [maybeHasControlCharsString length])];
NSLog(@"%@", noControlCharsString);
// Output: abcdef
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

Try:

NSString *noControlCharsString = [maybeHasControlCharsString stringByTrimmingCharactersInSet:controlCharacterSet];

EDIT As Martin R rightly says the above only removes any leading or trailing matches.

Here's a category method which strips all matches from the string:

- (NSString *)stringByStrippingCharactersInSet:(NSCharacterSet *)set {
    NSString *r = [self stringByTrimmingCharactersInSet:set];
    NSArray *components = [r componentsSeparatedByCharactersInSet:set];
    components = [components filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self <> ''"]];
    return [components componentsJoinedByString:@""];
}
ijwelch
  • 9
  • 2
  • `stringByTrimmingCharactersInSet` removes only *leading* and *trailing* characters, so `"\nA\nB\n"` would become `"A\nB"`. The middle control character is not replaced. – Martin R May 28 '13 at 07:31
  • Thanks Martin. Yes my first ever contribution and it was wrong:( – ijwelch May 28 '13 at 07:56
  • Welcome to SO! Your new code looks correct, you could even remove the call to `stringByTrimmingCharactersInSet`. - (I must admit that I am not a big fan of using `componentsSeparatedByCharactersInSet` to filter a string, because it creates a lot of temporary objects.) – Martin R May 28 '13 at 08:12
  • Actually the `filteredArrayUsingPredicate` call is also not necessary. – Martin R May 28 '13 at 11:40