iOS简单的字串替换方法stringByTrimmingCharactersInSet

本文介绍了一个用于过滤字符串中特定字符类型的简单方法,并指出其在处理括号和空格等特殊字符时的局限性。通过使用Objective-C语言实现的过滤逻辑,文章深入分析了不同字符集的过滤效果,并提出了改进方向。尽管存在不足,但该方法为理解字符串处理提供了实用的入门知识。

此方法只能过滤掉首尾,但是条件是集合

今天听人介绍了一个比较简单的过滤方法...不多说了,直接上代码

NSString *str = @"一个 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 汉0932字#@!中..文//>?输~~~@#$#@%#^#^%&^*&(*)入";
    NSMutableCharacterSet *set = [[NSMutableCharacterSet alloc] init];
    [set formUnionWithCharacterSet:[NSCharacterSet lowercaseLetterCharacterSet]];//小写字母
    NSLog(@"1 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet uppercaseLetterCharacterSet]];//大写字母
    NSLog(@"2 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];//符号
    NSLog(@"3 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];//标点
    NSLog(@"4 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet controlCharacterSet]];//控制符
    NSLog(@"5 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];//小数
    NSLog(@"6 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet letterCharacterSet]];//文字
    NSLog(@"7 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet nonBaseCharacterSet]];//非基础
    NSLog(@"8 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];//字母数字
    NSLog(@"9 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet decomposableCharacterSet]];//可分解
    NSLog(@"10 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet illegalCharacterSet]];//非法
    NSLog(@"11 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet capitalizedLetterCharacterSet]];//大写
    NSLog(@"12 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet newlineCharacterSet]];//换行符
    NSLog(@"13 %@",[str stringByTrimmingCharactersInSet:set]);
    [set formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//空格换行
    NSLog(@"14 %@",[str stringByTrimmingCharactersInSet:set]);

2012-07-26 19:11:01.863 yingkong1987[12876:fb03] 1 一个 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 汉0932字#@!中..文//>?输~~~@#$#@%#^#^%&^*&(*)入
2012-07-26 19:11:01.864 yingkong1987[12876:fb03] 2 一个 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 汉0932字#@!中..文//>?输~~~@#$#@%#^#^%&^*&(*)入
2012-07-26 19:11:01.864 yingkong1987[12876:fb03] 3 一个 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 汉0932字#@!中..文//>?输~~~@#$#@%#^#^%&^*&(*)入
2012-07-26 19:11:01.864 yingkong1987[12876:fb03] 4 一个 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 汉0932字#@!中..文//>?输~~~@#$#@%#^#^%&^*&(*)入
2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 5 一个 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 汉0932字#@!中..文//>?输~~~@#$#@%#^#^%&^*&(*)入
2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 6 一个 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 汉0932字#@!中..文//>?输~~~@#$#@%#^#^%&^*&(*)入
2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 7  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 8  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 9  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
2012-07-26 19:11:01.866 yingkong1987[12876:fb03] 10  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
2012-07-26 19:11:02.170 yingkong1987[12876:fb03] 11  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
2012-07-26 19:11:02.170 yingkong1987[12876:fb03] 12  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
2012-07-26 19:11:02.170 yingkong1987[12876:fb03] 13  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
2012-07-26 19:11:02.170 yingkong1987[12876:fb03] 14

发现这个方法有一个很大的缺点..比如要去掉(ABC)的英文部分..必须要先去掉外面的括号....不然无法过滤...

空格亦是如此

如果有更好的过滤方法,欢迎留言讨论


转自:IOS简单的字串替换方法stringByTrimmingCharactersInSet

内容概要:本文提出了一种基于非合作博弈理论的居民负荷分层调度模型,并结合双层鲸鱼优化算法(Two-level Whale Optimization Algorithm)进行高效求解,模型与算法均通过Matlab代码实现。研究针对电力系统中居民侧用电负荷的复杂调度问题,引入非合作博弈机制刻画各用户之间的利益竞争关系,实现负荷的分层优化分配;同时设计双层优化架构,上层优化资源配置,下层模拟用户自主决策行为,提升了模型的实用性与合理性。通过智能优化算法求解多层级、非凸非线性的博弈模型,有效提高了调度方案的收敛性与全局寻优能力,适用于现代智能电网中的需求侧管理与能源优化场景。; 适合人群:具备电力系统基础理论知识和Matlab编程能力,从事智能电网、能源优化调度、需求侧管理、博弈论应用等方向的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①应用于居民区电力负荷的分层优化调度系统设计与仿真分析;②为非合作博弈在多主体能源系统建模中的应用提供方法论支持;③利用双层鲸鱼算法解决具有嵌套结构的复杂双层优化问题,提升求解效率与调度方案的可行性。; 阅读建议:建议读者结合提供的Matlab代码深入理解模型构建逻辑与算法实现流程,重点关注博弈模型的效用函数设计、纳什均衡求解思路以及双层优化结构的迭代机制,宜配合实际用电数据开展复现实验以验证模型有效性与鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值