iOS字符串处理_替换(去掉空格换行)、截取
以下代码主要实现了:1、截取"@@"前的字符串; 2、去掉字符串中的"##"; 3、去掉字符串中的空格和换行。
希望相互学习相互指正。
-----ViewController.m内容如下:------
#import "ViewController.h"
#import "HandleString.h"
@interface ViewController ()
{
NSString *_str;
NSString *_tempStr;
UILabel *_label;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma -----1.截取@@前面的内容 2.去掉## 3.去掉空格换行
_str = @"静夜思##\n床前 明月光\n##疑是 ##地上霜\n举头@@ 望明月\n低头 思故乡";
_tempStr = _str;
[self createSubview];
}
- (void)createSubview{
_label = [[UILabel alloc]init];
CGRect temp = self.view.frame;
temp.origin.x += 20;
temp.origin.y += 80;
temp.size.width -= 40;
temp.size.height = 150;
_label.frame = temp;
_label.textAlignment = NSTextAlignmentCenter;
_label.lineBreakMode = NSLineBreakByWordWrapping;
_label.numberOfLines = 0;
_label.text = _str;
_label.textColor = [UIColor greenColor];
_label.backgroundColor = [[UIColor blueColor]colorWithAlphaComponent:0.3];
[self.view addSubview:_label];
for (NSInteger i = 0; i<4; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(0, CGRectGetMaxY(_label.frame)+50*(i+1), 150, 40);
CGRect a = btn.frame;
a.origin.x = self.view.center.x - a.size.width/2;
btn.frame = a;
btn.backgroundColor = [UIColor cyanColor];
NSArray *arr = @[@"截取@@前面的内容",@"去掉##",@"去掉空格换行",@"还原"];
[btn setTitle:arr[i] forState:UIControlStateNormal];
btn.tag = 10 + i;//tag分别为10,11,12,13
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
- (void)click:(UIButton *)button{
NSInteger x = button.tag;
switch (x) {
case 10:
_tempStr = [HandleString handleString:_tempStr interceptFrom:nil to:@"@"];
break;
case 11:
_tempStr = [HandleString handleString:_tempStr replace:@"##" with:@" "];
break;
case 12:
_tempStr = [HandleString delSpaceAndNewline:_tempStr];
break;
case 13:
_tempStr = _str;
break;
default:
break;
}
_label.text = _tempStr;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
------HandleString.h的内容如下:---------
#import <Foundation/Foundation.h>
@interface HandleString : NSObject
//用str2替换str1
+ (NSString *)handleString:(NSString *)string replace:(NSString *)str1 with:(NSString *)str2;
//从str1(包括),截取字符串到str2(不包括)
+ (NSString *)handleString:(NSString *)string interceptFrom:(NSString *)str1 to:(NSString *)str2;
//去掉字符串中的空格、换行
+ (NSString *)delSpaceAndNewline:(NSString *)string;
@end
------HandleString.m的内容如下:---------
#import "HandleString.h"
@implementation HandleString
+ (NSString *)delSpaceAndNewline:(NSString *)string;{
NSMutableString *mutStr = [NSMutableString stringWithString:string];
NSRange range = {0,mutStr.length};
[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
NSRange range2 = {0,mutStr.length};
[mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
return mutStr;
// string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
// string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
// string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
// return string;
}
#pragma -----如果字符串中有str1,用str2替换掉
+ (NSString *)handleString:(NSString *)string replace:(NSString *)str1 with:(NSString *)str2;{
if (str2 == nil) {
str2 = @"";
}
// //方式一
// NSMutableString *tempStr = [NSMutableString stringWithString:string];
// NSRange range = {0,tempStr.length};
// [tempStr replaceOccurrencesOfString:str1 withString:str2 options:NSLiteralSearch range:range];
// return tempStr;
// //方式二
// string = [string stringByReplacingOccurrencesOfString:str1 withString:str2];
// return string;
//方式三
NSArray *array = [string componentsSeparatedByString:str1];
NSInteger count = [array count] - 1;
NSMutableString *tempStr = [NSMutableString stringWithString:string];
for (NSInteger i = 0; i<count; i++) {
NSRange range = [tempStr rangeOfString:str1];
NSInteger location = range.location;
NSInteger length = range.length;
if (location != NSNotFound) {
[tempStr replaceCharactersInRange:NSMakeRange(location, length) withString:str2];
}
}
return tempStr;
}
#pragma ------从字符串str1,截取到str2
+ (NSString *)handleString:(NSString *)string interceptFrom:(NSString *)str1 to:(NSString *)str2;{
if (str1 == nil) {
str1 = @"";
}
if (str2 == nil) {
str2 = @"";
}
NSRange range1 = [string rangeOfString:str1];
NSInteger location1 = range1.location;
if (location1 != NSNotFound) {
string = [string substringFromIndex:location1];
}
NSRange range2 = [string rangeOfString:str2];
NSInteger location2 = range2.location;
if (location2 != NSNotFound) {
string = [string substringToIndex:location2];
}
return string;
}
@end
END
iOS字符串处理_替换(去掉空格换行)、截取的更多相关文章
- python字符串的操作(去掉空格strip(),切片,查找,连接join(),分割split(),转换首字母大写, 转换字母大小写...)
#可变变量:list, 字典#不可变变量:元祖,字符串字符串的操作(去掉空格, 切片, 查找, 连接, 分割, 转换首字母大写, 转换字母大小写, 判断是否是数字字母, 成员运算符(in / not ...
- SQLServer如何删除字段中的某个字符串,或者替换为空格?
sql="update Table set 字段=REPLACE ( 字段,'123' , ' ') where XXX条件"把字段中123替换为空格
- Notepad++查找和替换空行/空格/换行
Notepad++查找和替换支持正则表达式,功能很强大,但比较复杂因此暂不研究 Notepad++使用正则表达式查找,首先需要勾选查找/替换窗口左下部的“正则表达式(E)”\r\n表示换行,其中\r表 ...
- sql中保留一位小数的百分比字符串拼接,替换函数,换行符使用
select num ,cast(round(convert(float,isnull((a.Sum_Num-d.Sum_Num),0))/convert(float,c.Sum_Store_Num ...
- mysql去掉空格换行符
http://blog.csdn.net/gt219/article/details/52038382
- iOS字符串NSString中去掉空格(或替换为某个字符串)
http://blog.sina.com.cn/s/blog_6f29e81f0101qwbk.html [问题描述] 今天请求服务器返回的字段中含有空格,这空格是服务器开发人员不小心往数 ...
- iOS开发之-- 字符串的操作,去掉某一个字符或者替换成其他字符
一个简单的操作,记录下: NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withS ...
- linux makefile字符串操作函数 替换subst、模式替换patsubst、去首尾空格strip、查找字符串findstring、过滤filter、反过滤filter-out、排序函数sort、取单词word、取单词串wordlist、个数统计words
1.1 字符操作函数使用 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 ...
- js去掉html标签和去掉字符串文本的所有的空格
去掉html标签的js <script> function delHtmlTag(str){ return str.replace(/<[^>]+>/g,"& ...
随机推荐
- Nacos集群配置实例(windows下测试)
1.首先 fork 一份 nacos 的代码到自己的 github 库,然后把代码 clone 到本地. git地址:https://github.com/alibaba/nacos.git 2.然后 ...
- 分享一个Vue数组赋值的错误
今天在写项目用到Vue的时候,遇到的一个问题,纠结了好一会,首先我的代码是这样的 有没有毛病!! 开始我感觉是没啥毛病啊,按照之前写Java代码的逻辑,我感觉这没一点毛病 . 但是它就是有毛病, 假 ...
- Spring Boot 整合 Druid
Spring Boot 整合 Druid 概述 Druid 是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和 SQL 解析器组成.该项目主要是为了扩展 JDBC 的一些限制,可以让程 ...
- 登录oracle数据库
1.windows (cmd)命令行登录: 下载命令行工具 点击这里进入官网下载,下载其中三个文件 instantclient-basic-windows.x64-19.3.0.0.0dbru.zip ...
- Springboot实现登录功能
SpringBoot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再 ...
- 将项目部署到github的方法
GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名GitHub. GitHub于2008年4月10日正式上线,除了Git代码仓库托管及基本的 We ...
- SpringMvc 跨域处理
导读 由于浏览器对于JavaScript的同源策略的限制,导致A网站(Ajax请求)不能通过JS去访问B网站的数据,于是跨域问题就出现了. 跨域指的是域名.端口.协议的组合不同就是跨域. http:/ ...
- python中bitmap的参数
Tkinter Bitmaps: 你会使用这个属性显示一个位图.有以下类型的可用位图. 你会使用这个属性显示一个位图.有以下类型的可用位图.: "error" "gray ...
- Fusion360_Generative Design 入门学习笔记
2019.12.17更新 初次见到衍生式设计的时候感觉非常惊艳,现在觉得这个功能就是个弟弟,只能做一些中看不中用的东西.这个方法的理论基础是拓扑优化,想做research的同学可参阅"如何入 ...
- Django ContentType 的使用
引入 一切优化,最终都是关于需求的优化.本文介绍需求确定之后的数据库表结构设计优化. 程序员应该都知道,编程是数据结构和算法的结合.所谓数据就是用户需要访问和操作的资源,比如购物类App里面的商品,图 ...