iOS笔记之NSString基础类(Objective-C)

2018-04-03  本文已影响0人  Raywf

2018.4.3
文中诸多内容属直接转载,若原作者要求删除,请联系我,立删。

1. 通过stringByTrimmingCharactersInSet去除字符串两端的特殊符号

原作者:BlackWolfSky
原文:通过stringByTrimmingCharactersInSet去除字符串两端的特殊符号

1、函数简介
苹果的NSString类提供了stringByTrimmingCharactersInSet方法过滤字符串两端的特殊符号,函数声明如下:
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
对于该函数苹果官方的说明为:Returns a new string made by removing from both ends of the receiver characters contained in a given character set.  通过官方说明我们知道该函数将字符串两端的与set集合中的成员匹配的字符过滤掉。
2、函数使用
该函数的入参是NSCharacterSet类型,所以使用该函数之前需要定义一个NSCharacterSet变量,当然也可以使用系统提供的NSCharacterSet常量,如whitespaceAndNewlineCharacterSet、whitespaceCharacterSet等
3、举例如下:
a、自定义一个NSCharacterSet, 包含需要去除的特殊符号
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"。。。"];
NSString *trimmedString = [string stringByTrimmingCharactersInSet:set];
trimmedString是过滤后的字符串
b、使用系统的集合
NSString *text1 = [TextField.text stringByTrimmingCharactersInSet:[NSCharacterSet
 whitespaceCharacterSet]];//除两端的空格
NSString *text = [TextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去除两段的空格和回车注释: 

注释:
1、whitespaceAndNewlineCharacterSet 
Returns a character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085). 
2、whitespaceCharacterSet
Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).



2.关于删去字符串中不想要的字符

a.如果只是需要简单删掉空格或者回车

NSString *string = @"Test string, please enter it";

/* 去掉字符串中的空格和换行 */
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];

/* 或者 */

NSMutableString *mutStr = [NSMutableString stringWithString:string];
NSRange range = {0, string.length};
[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
range = {0, mutStr.length};
[mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range];
string = [mutStr copy];

b.删去稍复杂一些的字符

NSString *string = @"Test string, please enter it";

/* 过滤不想要的(非法)字符,filterString自定义 */
NSString *filterString = @"[]{}(#%-*+=_)\\|~(<>$%^&*)_+ ";
filterString = @" \n";      /* 示例:仅处理空格和换行 */
NSCharacterSet *filterSet = [NSCharacterSet characterSetWithCharactersInString:filterString];
string = [[string componentsSeparatedByCharactersInSet:filterSet] componentsJoinedByString:@""];



3.字符串替换的姿势

姿势1 - 针对多语言版本

/* 请将智能设备靠近手机并保持开机状态
   Please keep the smart device close to the phone and keep it turned on

   "请将智能设备%@手机\n并保持%@状态" = "Please keep the smart device %@ the phone and keep it %@";
   "靠近" = "close to";
   "开机" = "turned on";
*/

NSString *specialString =  @"<*#*>";
NSString *initString = [NSString stringWithFormat:@"请将智能设备%@手机\n并保持%@状态", specialString, specialString];
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
                             NSForegroundColorAttributeName:[UIColor lightGrayColor]};

NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]
                                             initWithString:initString attributes:attributes];

NSRange range1 = [attributedText.string rangeOfString:specialString];
attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
               NSForegroundColorAttributeName:[UIColor blackColor]};
[attributedText replaceCharactersInRange:range1
                    withAttributedString:[[NSAttributedString alloc]
                                          initWithString:@"靠近" attributes:attributes]];

NSRange range2 = [attributedText.string rangeOfString:specialString];
attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
               NSForegroundColorAttributeName:[UIColor blackColor]};
[attributedText replaceCharactersInRange:range2
                    withAttributedString:[[NSAttributedString alloc]
                                          initWithString:@"开机" attributes:attributes]];

NSString *resultString = attributedText.string;
上一篇下一篇

猜你喜欢

热点阅读