国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

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

2020-02-19 15:53:30
字體:
來源:轉載
供稿:網友

IOS開發過程中,我們可能會遇到許多關于字符串的編程,字符串是一種常見的數據類型,以下是武林技術頻道小編介紹的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 = ( ///"http:///", ///"http:///", ///"http:///", ///"http:////U8ba8////U538c////U7684////U8282////U70b9///", ///"http:///", ///"http:///", ///"http:///", ///"http:///", ///"http:///", ///"http:///", ///"http:///", ///"http:///", ///"http:///")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(@///"http:////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(@///"http:////ncurrent = %@///",string);
???????????????
//截取日期substringToIndex
NSString *currentDate = [string substringToIndex:10];
????????????????
NSLog(@///"http:////ncurrentDate = %@///",currentDate);


打印結果:

?

?

?

current = 2016-01-1711:12currentDate = 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(@///"http:////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 STRINGstring:stringstring: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類操作字符串的方法有哪些,相信大家都有了一定的了解,想要了解更多的技術信息,請繼續關注武林技術頻道吧!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 虎林市| 固安县| 临泉县| 醴陵市| 隆子县| 信阳市| 瑞安市| 房山区| 双流县| 寿阳县| 丰县| 漳州市| 沂源县| 靖远县| 通许县| 南岸区| 永定县| 历史| 宜都市| 拜泉县| 新竹市| 汉阴县| 循化| 曲沃县| 彰化县| 蕲春县| 富源县| 龙海市| 虹口区| 襄城县| 化州市| 富平县| 镇雄县| 丰都县| 沐川县| 老河口市| 鸡泽县| 大丰市| 鄢陵县| 安图县| 左云县|