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