iOS字串處理_替換(去掉空格換行)、截取,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