- 目前,有不少文献介绍字符串的一些基本操作,如:字符串插入、字符串覆盖、字符串截取、分割字符串。
但有时候,仅仅停留在这些基本操作还不能直接满足一些需求,这时候可以利用这些基本操作进行一些字符串的衍生操作。
这篇文章,将先引用CSDN一个大神的字符串【基本操作】,基于此,然后介绍一些【字符串的衍生操作】。
一. 字符串的基本操作
1.【字符串插入】
NSMutableString* str1=[[NSMutableString alloc] initWithString:@"hello"];//存在堆区,可变字符串
NSLog(@"str1:%@",str1);
[str1 insertString:@"234"atIndex:2];//把一个字符串插入另一个字符串中的某一个位置
NSLog(@"str1:%@",str1);
2.【字符串覆盖】
NSRange range={1,2};//字符串覆盖另一个字符串(覆盖范围可以设定)
[str1 replaceCharactersInRange:range withString:@"dffdf"];
NSLog(@"str1:%@",str1);
3.【字符串截取】
NSMutableString* str4=[[NSMutableString alloc] initWithString:@"handaiy"];
NSString* str5=[str4 substringFromIndex:5];//截取从第5位到最后一位
NSLog(@"截取后的字符串为:%@",str5);
NSMutableString* str6=[[NSMutableString alloc] initWithString:@"handaiy"];
NSString* str7=[str6 substringToIndex:5];//截取从0位到第4位
NSLog(@"截取后的字符串为:%@",str7);
NSMutableString* str8=[[NSMutableString alloc] initWithString:@"handaiy"];
NSRange ange={2,3};
NSString* str9=[str8 substringWithRange:ange];//截取从第二位到第4位
NSLog(@"截取后的字符串为:%@",str9);
4.【字符串的大小转换】
/*
(1)将所有的字符串内容变为大写字母 uppercaseString
(2)将所有的字符串内容变为小写字母 lowercaseString
(3)将单词的首字母小写变为大写字母 capitalizedString
*/
NSString* str10=@"sayhelloworld";
NSString* str11=[str10 uppercaseString];
NSLog(@"str11:%@",str11);//(1)将所有的字符串内容变为大写字母
NSString* str12=@"Hello WORD";
NSString* str13=[str12 lowercaseString];
NSLog(@"str11:%@",str13);//(2)将所有的字符串内容变为小写字母
NSString* stu=@"sayhelloworld";
NSString* aa=[stu substringToIndex:3];//截取say
NSString* stu1=[aa capitalizedString];//把say首字母变成大写
NSRange rang={3,5};//截取hello
NSString* bb=[stu substringWithRange:rang];//取出hello创建对象bb
NSString* stu2=[bb capitalizedString];//把hello首字母变成大写
NSString* dd=[stu substringFromIndex:8];
NSString* stu3=[dd capitalizedString];
//拼接方法(1)
NSString* stu4=[NSString stringWithFormat:@"%@%@%@",stu1,stu2,stu3];//把截取出来的单词拼接成新的字符串。
NSLog(@"截取单词后,每个单词首字母变成大写,然后重新拼接成的新字符串:%@",stu4);//(4)字符串截取、改变、拼接
//拼接方法(2)如:
NSString* h=@"2014年";
NSString* j=@"9月";
NSString* k=@"26号";
NSString* riqi=[[[[h stringByAppendingString:@"_"]
stringByAppendingString:j]
stringByAppendingString:@"_"]
stringByAppendingString:k];
NSLog(@"%@",riqi);
```
5.【分割字符串】
NSString* nba=@"2014.9.26";
NSArray *p=[nba componentsSeparatedByString:@"."];
NSLog(@"分割后的字符串:%@",p);
```
6.【字符串转化为数字型再计算】intValue、floatVale...
NSString* cba=@"2014";
int s=[cba intValue];
int d=s+1;
NSLog(@"字符串转化成数字后再计算:%d",d);
7.【数字型转化为字符串】
int z=45534;
NSString* g=[NSString stringWithFormat:@"%d",z];
NSLog(@"数字转化为字符串:%@",g);
8.字符串的【末尾追加】新的字符串
NSString *bbc =@"www.cn";
NSString *bbc1 = [bbc stringByAppendingString:@"itcast.cn"];
NSLog(@"bbc1: %@",bbc1);
9.在【制定的范围追加】字符串(插入字符串、替换字符串)
NSString *aac =@"wwwbbbccc";
NSRange ranges = {4,0};
NSString *aac1 = [aac stringByReplacingCharactersInRange:ranges withString:@"aa"];
NSLog(@"aac1: %@",aac1);
10.使用新的字符串【替换】原有的字符串或删除指定的字符串
NSString *cca =@"ww w.bai.com";
NSString *cca1 = [cca stringByReplacingOccurrencesOfString:@"bai"withString:@"du"];//替换
NSString *cca2 = [cca stringByReplacingOccurrencesOfString:@"co"withString:@""];//删除
NSString *cca3 = [ccastringByReplacingOccurrencesOfString:@" "withString:@""];//删除空格号
NSLog(@"cca1: %@ cca2: %@ cca3: %@",cca1,cca2,cca3);
11.【带引号】的字符串
NSString *string = @"hello, \"world\"";
NSLog(@"带引号的字符串:%@",string);
NSString *string2 = [NSString stringWithFormat:@"hello,\"%@\"",@"世界"];
NSLog(@"拼接字符串:%@",string2);
- 上面输出效果:
2017-06-10 11:37:07.535423 test123[1588:130648] 带引号的字符串:hello, "world"
2017-06-10 11:37:07.535488 test123[1588:130648] 拼接字符串:hello,"世界"
Program ended with exit code: 0
二. 字符串(NSString)与数值(int,NSInteger)的基本转换
假设有
NSString *tempA = @"123";
NSString *tempB = @"456";
1. 字符串拼接
NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];
2. 字符转NSInteger
NSInteger k = [newString integerValue];
3. NSInteger转字符
NSString *s = [NSString stringWithFormat:@"%ld",k];
4. 字符转int
int intString = [newString intValue];
5. int转字符
NSString *stringInt = [NSString stringWithFormat:@"%d",intString];
6. 字符转float
float floatString = [newString floatValue];
7. float转字符
NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];
三. 字符串的衍生操作
1.首字母替换为星号*(脱敏处理)
- 需求:
脱敏处理,比如隐藏某个字符串的首字符为*号。
- 功能拆解:
先【截取】指定字符串首字符,作为占位字符串【替换】为指定字符串。
- 代码
NSString *testStr = @"我的名字很敏感";
NSString *tempStr = [testStr substringToIndex:1];
_headerView.nickNameLabel.text = [testStr stringByReplacingOccurrencesOfString:tempStr withString:@"*"];
2.检查中文字符
- 需求
检查字符串中是否有中文
- 代码
#pragma mark - 检查中文输入
- (BOOL)validateChinese
{
NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{2,8}$";
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
return [passWordPredicate evaluateWithObject:self];
}
3.字符串过滤emoji表情符
- 需求
用户输入了一个文本,当需要先出该文本字符串过滤emoji表情符才能进行下一步操作时,如作为网络请求参数。一些后台的数据库不支持emoji的处理。
- 代码
#pragma mark - @return 没有表情符号的字符串.
- (NSString *)emojizedStringByReplacingOccurrencesOfString:(NSString *)text
{
if (self.length == 0) {
return text;
}
__block NSString *resultStr = @"";
[self enumerateSubstringsInRange:NSMakeRange(0,
[self length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring,
NSRange substringRange,
NSRange enclosingRange,
BOOL *stop)
{
// NSLog(@"substring: %@", substring);
BOOL containsEmoji = NO;
NSString *contains = substring;
const unichar hs = [substring characterAtIndex:0];
// surrogate pair
if (0xd800 <= hs &&
hs <= 0xdbff)
{
if (substring.length > 1)
{
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc &&
uc <= 0x1f9c0)
{
containsEmoji = YES;
contains = text;
}
}
}
else if (substring.length > 1)
{
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3 ||
ls == 0xfe0f ||
ls == 0xd83c)
{
containsEmoji = YES;
contains = text;
}
}
else
{
// non surrogate
if (0x2100 <= hs &&
hs <= 0x27ff)
{
containsEmoji = YES;
contains = text;
}
else if (0x2B05 <= hs &&
hs <= 0x2b07)
{
containsEmoji = YES;
contains = text;
}
else if (0x2934 <= hs &&
hs <= 0x2935)
{
containsEmoji = YES;
contains = text;
}
else if (0x3297 <= hs &&
hs <= 0x3299)
{
containsEmoji = YES;
contains = text;
}
else if (hs == 0xa9 ||
hs == 0xae ||
hs == 0x303d ||
hs == 0x3030 ||
hs == 0x2b55 ||
hs == 0x2b1c ||
hs == 0x2b1b ||
hs == 0x2b50)
{
containsEmoji = YES;
contains = text;
}
}
resultStr = [NSString stringWithFormat:@"%@%@", resultStr, contains];
}];
return resultStr;
}
【后续待更新..】
参考文献
http://blog.csdn.net/sevenquan
http://www.cnblogs.com/Zsmile/p/4140371.html