欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Objective-C中使用NSString类操作字符串的方法小结

程序员文章站 2023-11-14 10:33:46
一、字符串切割 1、带节点的字符串,如@"

讨厌的节点

"我们只想要中间的中文 处理方法: 复制代码 代码如下...

一、字符串切割
1、带节点的字符串,如@"<p>讨厌的节点<br/></p>"我们只想要中间的中文

处理方法:

复制代码 代码如下:

nsstring *string1 = @"<p>讨厌的节点<br/></p>";
 
/*此处将不想要的字符全部放进characterset1中,不需另外加逗号或空格之类的,除非字符串中有你想要去除的空格,此处< p /等都是单独存在,不作为整个字符*/
 
nscharacterset *characterset1 = [nscharacterset charactersetwithcharactersinstring:@"<p/brh>"];
 
// 将string1按characterset1中的元素分割成数组
 
nsarray *array1 = [string1 componentsseparatedbycharactersinset:characterset1];
 
nslog(@"array = %@",array1);
 
for(nsstring *string1 in array1)
{
    if ([string1 length]>0) {
        
        // 此处string即为中文字符串
 
        nslog(@"string = %@",string1);
    }
}

打印结果:
2016-01-17 10:55:34.017 string[17634:303] 
array = (
 "",
 "",
 "",
 "\u8ba8\u538c\u7684\u8282\u70b9",
 "",
 "",
 "",
 "",
 "",
 "",
 "",
 "",
 ""
)
2016-01-17 10:55:34.049 string[17634:303] 
string = 讨厌的节点

2、带空格的字符串,如

@"hello world"去掉空格

复制代码 代码如下:

nsstring *string2 = @"hello world";
 
/*处理空格*/
 
nscharacterset *characterset2 = [nscharacterset whitespacecharacterset];
 
// 将string1按characterset1中的元素分割成数组
nsarray *array2 = [string2 componentsseparatedbycharactersinset:characterset2];
 
nslog(@"\narray = %@",array2);
 
// 用来存放处理后的字符串
nsmutablestring *newstring1 = [nsmutablestring string];
 
for(nsstring *string in array1)
{
    [newstring1 appendstring:string];
}
nslog(@"newstring = %@", newstring1);

打印结果:
2016-01-17 11:02:49.656 string[17889:303] 
array = (
 hello,
 world
)
2016-01-17 11:02:49.657 string[17889:303] newstring = helloworld

ps:处理字母等其他元素只需将nscharacterset的值改变即可。

复制代码 代码如下:

+ (id)controlcharacterset;
 
+ (id)whitespacecharacterset;
 
+ (id)whitespaceandnewlinecharacterset;
 
+ (id)decimaldigitcharacterset;
 
+ (id)lettercharacterset;
 
+ (id)lowercaselettercharacterset;
 
+ (id)uppercaselettercharacterset;
 
+ (id)nonbasecharacterset;
 
+ (id)alphanumericcharacterset;
 
+ (id)decomposablecharacterset;
 
+ (id)illegalcharacterset;
 
+ (id)punctuationcharacterset;
 
+ (id)capitalizedlettercharacterset;
 
+ (id)symbolcharacterset;
 
+ (id)newlinecharacterset ns_available(10_5, 2_0);
 
+ (id)charactersetwithrange:(nsrange)arange;
 
+ (id)charactersetwithcharactersinstring:(nsstring *)astring;
 
+ (id)charactersetwithbitmaprepresentation:(nsdata *)data;
 
+ (id)charactersetwithcontentsoffile:(nsstring *)fname;

二、用字符将nsarray中的元素拼接起来

复制代码 代码如下:

nsarray *array = [nsarray arraywithobjects:@"hello",@"world",nil];
 
//如要用,:等字符串拼接,只需将下面的@" "空格换成@","或@":"即可
nsstring *string = [array componentsjoinedbystring:@" "];
 
nslog(@"string = %@",string);

打印结果:
hello world

三、截取子串:

这里以获取时间为例,利用nsdate获取到当前时间时,有时候只需要日期或者只需要时间

1、从字符串开头截取到指定的位置,如

复制代码 代码如下:

//获取到当前日期时间   
nsdate *date = [nsdate date];
        
//定义日期格式,此处不重点讨论nsdate,故不详细说明,在后面会详细讨论      
nsdateformatter *dateformatter = [[nsdateformatter alloc] init];
        
//设置日期格式       
[dateformatter setdateformat:@"yyyy-mm-dd hh:mm"];
        
//将日期转换成nsstring类型     
nsstring *string = [dateformatter stringfromdate:date];
nslog(@"\ncurrent = %@",string);
               
//截取日期substringtoindex
nsstring *currentdate = [string substringtoindex:10];
                
nslog(@"\ncurrentdate = %@",currentdate);

打印结果:
current = 2016-01-1711:12


currentdate = 2016-01-17

2、抽取中间子串-substringwithrange

复制代码 代码如下:

//截取月日
nsstring *currentmonthanddate = [string substringwithrange:[nsmakerange(5, 5)]];
        
nslog(@"currentmonthanddate = %@",currentmonthanddate);

打印结果:
currentmonthanddate = 06-27

3、从某一位置开始截取- substringfromindex

复制代码 代码如下:

//截取时间substringfromindex
nsstring *currenttime = [string substringfromindex:11];
        
nslog(@"\ncurrenttime = %@",currenttime);\

打印结果:
currenttime = 11:25

四、比较字符串

复制代码 代码如下:

nsstring *first = @"string";
nsstring *second = @"string";

1、判断两个字符串是否相同-isequaltostring方法
复制代码 代码如下:

bool isequal = [first isequaltostring:second];
 
nslog(@"first is equal to second:%@",isequal);

打印结果:
first is equal to second:0

2、compare方法比较字符串三个值

复制代码 代码如下:

nsorderedsame//是否相同
nsorderedascending//升序,按字母顺序比较,大于为真
nsordereddescending//降序,按字母顺序比较,小于为真

bool result = [first compare:sencond] == nsorderedsame;   
nslog(@"result:%d",result);


打印结果:
result:0 

复制代码 代码如下:

bool result = [first compare:second] == nsorderedascending;   
nslog(@"result:%d",result);


打印结果:
result:0

复制代码 代码如下:

bool result = [first compare:second] == nsordereddecending; nslog(@"result:%d",result);

打印结果:

result:1

3、不考虑大小写比较字符串

复制代码 代码如下:

bool result = [first compare:second
                     options:nscaseinsensitivesearch | nsnumericsearch] == nsorderedsame;
nslog(@"result:%d",result);

打印结果:
result:1

五、改变字符串大小写

复制代码 代码如下:

nsstring *astring = @"a string";
nsstring *string = @"string";
//大写
nslog(@"astring:%@",[astring uppercasestring]);
//小写
nslog(@"string:%@",[string lowercasestring]);
//首字母大小写
nslog(@"string:%@",[string capitalizedstring]);

打印结果:
astring:a string

string:string

string:string

六、在字符串中搜索子串

复制代码 代码如下:

nsstring *string1 = @"this is a string";
nsstring *string2 = @"string";
nsrange range = [string1 rangeofstring:string2];
nsuinteger location = range.location;
nsuinteger leight = range.length;
nsstring *astring = [[nsstring alloc] initwithstring:[nsstring stringwithformat:@"location:%li,leight:%li",location,leight]];
nslog(@"astring:%@",astring);
[astring release];

打印结果:
astring:location:10,leight:6