今天就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结尾");
}