这适用于常规NSString
:
NSString *newString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];
但是没有这样的方法NSMutableAttributedString
。我怎样才能删除一个逗号的所有实例NSMutableAttributedString
?
这适用于常规NSString
:
NSString *newString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];
但是没有这样的方法NSMutableAttributedString
。我怎样才能删除一个逗号的所有实例NSMutableAttributedString
?
let attrString = NSMutableAttributedString(string: "Hello <b>friend<b>")
attrString.mutableString.replaceOccurrencesOfString("<b>", withString: "", options: NSStringCompareOptions.CaseInsensitiveSearch, range: NSRange(location: 0, length: attrString.length))
尝试这个 :)
在创建属性字符串之前执行此操作,如果可以或取决于您获取它的方式。如果你不能,那么你可以使用replaceCharactersInRange:withString:
(或replaceCharactersInRange:withAttributedString:
),但你需要知道范围,所以你需要自己搜索和迭代。
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)];
您可以使用设计的 init 使用剥离的字符串初始化属性字符串。不?
该代码可以从我的回答中应用:
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];
}