Objective-C中使用NSString类操作字符串的方法有哪些

IOS开发过程中,我们可能会遇到许多关于字符串的编程,字符串是一种常见的数据类型,以下是中国E盟技术频道小编介绍的Objective-C中使用NSString类操作字符串的方法有哪些,一起进入下文了解一下吧!

一、字符串切割
1、带节点的字符串,如@\\\"

讨厌的节点

\\\"我们只想要中间的中文

处理方法:


NSString *string1 = @\\\"

讨厌的节点

\\\";

/*此处将不想要的字符全部放进characterSet1中,不需另外加逗号或空格之类的,除非字符串中有你想要去除的空格,此处
NSCharacterSet *characterSet1 = [NSCharacterSet characterSetWithCharactersInString:@\\\"

\\\"];

// 将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

上文是关于Objective-C中使用NSString类操作字符串的方法有哪些,相信大家都有了一定的了解,想要了解更多的技术信息,请继续关注中国E盟技术频道吧!