Remove characters from NSString?

后端 未结 6 2152
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 18:01
NSString *myString = @\"A B C D E F G\";

I want to remove the spaces, so the new string would be \"ABCDEFG\".

6条回答
  •  伪装坚强ぢ
    2020-11-29 18:33

    if the string is mutable, then you can transform it in place using this form:

    [string replaceOccurrencesOfString:@" "
                            withString:@""
                               options:0
                                 range:NSMakeRange(0, string.length)];
    

    this is also useful if you would like the result to be a mutable instance of an input string:

    NSMutableString * string = [concreteString mutableCopy];
    [string replaceOccurrencesOfString:@" "
                            withString:@""
                               options:0
                                 range:NSMakeRange(0, string.length)];
    

提交回复
热议问题