今天就NSString的各種語法學習學習,以後慢慢補充;
1.字符串的遍歷
NSString *string = @"CHENGWULI";
//字符串的長度
int count = [string length];
NSLog(@"字符串的長度是%d",count);
//遍歷字符串中的每一個字符
for(int i =0; i < count; i++){
char c = [string characterAtIndex:i];
NSLog(@"字符串第 %d位為 %c",i,c);
}
2.字符串的比較
NSString *str0 = @"lichengwu";
NSString *str1 = @"lichengwu-A";
//字符串完全相等比較
if([str0 isEqualToString:str1]){
NSLog(@"字符串完全相等");
}
else{
NSLog(@"字符串不相等");
}
//字符串以開頭比較
if([str0 hasPrefix:@"li"]){
NSLog(@"字符串str0以li開頭");
}
//字符串以結尾比較
if([str1 hasSuffix:@"wu-A"]){
NSLog(@"str1字符串以wu-A結尾");
}