3

这适用于常规NSString

NSString *newString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];

但是没有这样的方法NSMutableAttributedString。我怎样才能删除一个逗号的所有实例NSMutableAttributedString

4

5 回答 5

7
let attrString = NSMutableAttributedString(string: "Hello <b>friend<b>")

attrString.mutableString.replaceOccurrencesOfString("<b>", withString: "", options: NSStringCompareOptions.CaseInsensitiveSearch, range: NSRange(location: 0, length: attrString.length))

尝试这个 :)

于 2016-07-21T13:13:19.807 回答
1

在创建属性字符串之前执行此操作,如果可以或取决于您获取它的方式。如果你不能,那么你可以使用replaceCharactersInRange:withString:(或replaceCharactersInRange:withAttributedString:),但你需要知道范围,所以你需要自己搜索和迭代。

于 2015-04-14T19:39:08.837 回答
1
NSString *newString= "I want to ,show, you how to achieve this";
NSMutableAttributedString *displayText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",newString]];
[[displayText mutableString] replaceOccurrencesOfString:@"," withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, displayText.string.length)];
于 2015-04-14T19:39:20.010 回答
0

您可以使用设计的 init 使用剥离的字符串初始化属性字符串。不?

于 2015-04-14T19:40:00.120 回答
0

该代码可以从我的回答中应用

NSAttributedString *attributedString = ...;
NSAttributedString *anotherAttributedString = ...; //the string or characters which will replace

while ([attributedString.mutableString containsString:@"replace"]) {
        NSRange range = [attributedString.mutableString rangeOfString:@"replace"];
        [attributedString replaceCharactersInRange:range  withAttributedString:anotherAttributedString];
    }
于 2016-03-25T09:07:51.110 回答