IOS 学习笔记 2015-03-24 OC-API-不可变字符串

时间:2022-02-15 19:41:39
大部分是模仿
//
// main.m
// OC-API-不可变字符串
//
// Created by wangtouwang on 15/3/25.
// Copyright (c) 2015年 wangtouwang. All rights reserved.
// #import <Foundation/Foundation.h> //初始化
void testInit(){
//创建C 字符串对象
char str0 ="C String";
//创建字符串对象 无需释放内存
NSString *str1 = @"MAIKE";
//创建字符串对象 需要释放内存
NSString *str2 = [[NSString alloc] initWithString : @"JACK" ];
NSString *str3 = [[NSString alloc] initWithFormat:@"LUCY %i",];
NSString *str4 = [[NSString alloc] initWithUTF8String:"C String"]; //以上方法都有对于的静态方法,也无需释放内存
str2 = [NSString stringWithString:@"STATIC JACK"];
str3 = [NSString stringWithFormat:@"STATIC LUCY %i",];
str4 = [NSString stringWithUTF8String:"C String"];
} int main(int argc, const char * argv[]) {
@autoreleasepool {
//转换成大写
NSLog(@"zhangjianhua 转换成 大写 %@",[@"zhangjianhua" uppercaseString]);
//转换成小写
NSLog(@"ZHANGJIANHUA 转换成 小写 %@",[@"ZHANGJIANHUA" lowercaseString]);
//首字母大写
NSLog(@"zhangjianhua 转换成 小写 %@",[@"zhangjianhua" capitalizedString]);
//比较字符串内容
NSLog(@" ABC isEqualToString Abc to result= %i",[@"ABC" isEqualToString:@"Abc"]);
//比较字符串内容 忽略大小写
NSComparisonResult result2= [@"abc" compare:@"aBc"];//如果是[@"abc" caseInsensitiveCompare:@"aBc"]则忽略大小写比较
if(result2==NSOrderedAscending){
NSLog(@"left<right.");
}else if(result2==NSOrderedDescending){
NSLog(@"left>right.");
}else if(result2==NSOrderedSame){
NSLog(@"left=right.");
}
//查询指定字符串在前缀什么位置
NSLog(@" ABCDEFJ BY CD in location %i",[@"ABCDEFJ" hasPrefix:@"CD"]);
//查询指定字符串在后缀什么位置
NSLog(@" ABCDEFJ BY FJ in location %i",[@"ABCDEFJ" hasSuffix:@"F"]);
//查询指定字符串在什么位置和长度
NSLog(@" ABCDEFJ BY CD in location %@", NSStringFromRange([@"ABCDEFJ" rangeOfString:@"CD"]));
//截取指定字符串位置 根据CGRange对象
NSLog(@"JAVA Subetring TO Result %@",[@"ABCDEFG" substringWithRange:NSMakeRange(, )]);
//截取指定字符串对象 根据下标 从0到了该位置就停止
NSLog(@"SubString String By Index %@",[@"ABCDEFG" substringToIndex:]);
//截取指定字符串对象 根据下标 从位置到结尾停止
NSLog(@"SubString String By Index %@",[@"ABCDEFG" substringFromIndex:]); //字符串分割
NSString *str9 = @"MY NAME IS WP,AGE=28";
NSArray *array = [str9 componentsSeparatedByString:@" "];
NSLog(@"%@",array); //内型转换
//转换成int
NSLog(@"%i",[@"" intValue]);
//转换成C语音字符串
const char *s=[@"abc" UTF8String];//转换为C语言字符串
NSLog(@"%s",s); NSLog(@"%c",[@"abc" characterAtIndex:]);//取出制定位置的字符 }
return ;
}