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

时间: 2024-10-23 20:33:44

iOS字符串处理_替换(去掉空格换行)、截取的相关文章

python字符串的操作(去掉空格strip(),切片,查找,连接join(),分割split(),转换首字母大写, 转换字母大小写...)

#可变变量:list, 字典#不可变变量:元祖,字符串字符串的操作(去掉空格, 切片, 查找, 连接, 分割, 转换首字母大写, 转换字母大小写, 判断是否是数字字母, 成员运算符(in / not in))字符串使用时用单引号或者双引号括起来: var1 = 'hello' var2 ="world" 字符串不能修改值, 如下例所示: 以下是字符串常用的一些操作. 1. 去掉空格str.strip() # 去掉两边的空格和换行符str.strip('a') # 去掉两边指定的字符st

SQLServer如何删除字段中的某个字符串,或者替换为空格?

sql="update Table set 字段=REPLACE ( 字段,'123' , ' ') where XXX条件"把字段中123替换为空格

nsstring 字符串去掉空格换行和括号

+ (NSString *)handleSpaceAndEnterElementWithString:(NSString *)sourceStr { NSString *realSre = [sourceStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSString *realSre1 = [realSre stringByReplacingOccurrencesOf

iOS字符串NSString中去掉空格(或替换为某个字符串)

http://blog.sina.com.cn/s/blog_6f29e81f0101qwbk.html [问题描述] 今天请求服务器返回的字段中含有空格,这空格是服务器开发人员不小心往数据中多敲了,所以在客户端需要把字符串中的空格过滤掉. [问题分析] 1.使用NSString中的stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]方法只是去掉左右两边的空格: 2.使用NSString *strUrl

NSString去掉空格和换行失败

从谷歌地图接口取回数据包含回车换行符号,由于需要须把空格和换行符去掉,正常来说应该把字符串内的\n替换就可以了,后来试了一下不行.百度了一下,有人说换行应该是\r\n,也有人说需要换成\\n,众说纷纭,全部试过均告失败: NSString *temp = [[NSString alloc]initWithData:xmlData encoding:NSUTF8StringEncoding]; [temp stringByReplacingOccurrencesOfString:@"\n"

【trim()】去掉字符串开头和结尾的空格,防止不必要的空格导致的错误。

去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String a=" abc"; String b="abc"; System.out.println(b.equals(a)); a=a.trim();//去掉字符串中的空格 System.out.println(a.equals(b)); }

js/jquery 去掉空格.回车.换行

Jquery:$("#accuracy").val($("#accuracy").val().replace(/\ +/g,""));//去掉空格 .replace(/\ +/g,"") //去掉空格方法$("#content").val($("#content").val().replace(/[ ]/g,""));    //去掉空格 .replace(/[ ]/

请实现一个函数,把字符串中的每一个空格替换成“%20”,比如输入 “We are Happly。” 则输出“we%20are%20happy。”

请实现一个函数,把字符串中的每一个空格替换成"%20",比如输入 "We are Happly."  则输出"we%20are%20happy. " void ReplaceBlank(char String[],int length) { //originalLength 为字符串string的实际长度 //length字符串的总容量 int originalLength  = 0; int numberOfBlank = 0; int i =

String 中去掉空格

JAVA中去掉空格 1. String.trim() trim()是去掉首尾空格 2.str.replace(" ", ""); 去掉所有空格,包括首尾.中间 复制代码 代码如下:String str = " hell o "; String str2 = str.replaceAll(" ", ""); System.out.println(str2); 3.或者replaceAll(" +&q