简体   繁体   English

删除 NSMutableAttributedString 中的特殊字符

[英]Remove special characters in NSMutableAttributedString

In my app to show text in CATextLayer (change colors for characters),using NSMutableAttributedString to change colors,i want remove special characters in NSMutableAttributedString to show PopUp view, but didn't know how to remove special characters, to help to solve problem在我的应用程序中显示CATextLayer文本(更改字符的颜色),使用NSMutableAttributedString更改颜色,我想删除NSMutableAttributedString中的特殊字符以显示弹出视图,但不知道如何删除特殊字符,以帮助解决问题

i want like this type of o/p我想要这种类型的 o/p

"code" to code  //in NSMutableAttributedString, not in NSString

to remove such characters you simply write something like this:要删除这些字符,您只需编写如下内容:

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

Note that symbol " is written as \\" - this one is called an escape sequence.请注意,符号“被写成\\”——这个被称为转义序列。 Here is a list of such special characters in C - http://msdn.microsoft.com/en-us/library/h21280bw(v=vs.80).aspx这是 C 中此类特殊字符的列表 - http://msdn.microsoft.com/en-us/library/h21280bw(v=vs.80).aspx

Try this...This may help you.试试这个……这可能对你有帮助。

NSMutableString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"] invertedSet];
NSMutableString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSLog (@"Result: %@", resultString);

You can give try to this may it fits in your code:您可以尝试一下,它可能适合您的代码:

 NSMutableString *mutableStrng = [NSMutableString stringWithCapacity:1000];
[mutableStrng setString:@"my name is i#pho#ne"];

[mutableStrng replaceOccurrencesOfString:@"#" withString:@"" options:0 range:NSMakeRange(0,
 mutableStrng.length)];
NSMutableAttributedString *mutableAtrString = [[NSMutableAttributedString
 alloc]initWithString:mutableStrng];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM