你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Objective-C中使用NSString類操作字符串的方法小結

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

編輯:IOS開發綜合

一、字符串切割
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

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved