- 创建常量字符串
NSString *string = @“This is a String!”;
- 创建空字符串,给予赋值
NSString *string = [[NSString alloc] init]; string = @“This is a String!”;
- 通过字符串创建字符串
[[NSString alloc] initWithString:@“This is a String!”]; //或者[[NSString alloc] initWithFormat:@“My age is %i”,19];
- 用标准c创建字符串
char *cString ="这是一个字符串”; [[NSString alloc] initWithString:cString encoding:NSUTF8StringEncoding]; 或者[[NSString alloc] initWithUTF8Sting:cString]; //字符串编码可以在NSString.h中查阅
NSString *str=[[NSString alloc] initWithString:@“this is a string”]; //这个创建str需要释放内存 NSString *str=[NSString stringWithString:@"this is a string"]; //这个创建str不需要释放内存
NSString *path=@“/User/desktop/test.txt”;
nsstring *str=[NSString stringWithContentOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *path =@“/User/desktop/test.txt”;
NSString *str=@"123";
NSError *error;
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
- -(NSString *)uppercaseString 全部转为大写字母
- -(NSString *)lowercaseString 全部转为小写字母
- -(NSString *)capitalizedString 首字母遍大写,其他字母都变小写
- -(BOOL)isEqualToString:(NSString *)aString
比较两个字符串的内容是否相同,相同就返回YES,不同就返回NO
- -(NSComparisonResult)compare:(NSString *)string
- -(NSComparisonResult)caseInsensitiveCompare:(NSString *)string
忽略大小写进行比较,返回值于compare:一致
- -(NSString *)substringFromIndex:(NSUInteger)from 从指定位置from开始(包括指定位置的字符)到尾部
- -(NSString *)substringToIndex:(NSUInteger)to 从字符串的开头一致截取到指定的位置to,但不包括该字符
- -(NSString *)substringWithRange:(NSRange)range 按照所给出的NSRange从字符串中截取子串
- -(NSArray *)componentsSeparatedByString:(NSString *)separator
- -(double)doubleValue
- -(float)floatValue
- -(int)intValue
- -(char *)UTF8String
NSMutableString *str=[[NSMutableString alloc]initWithCapacity:10]; [str setString:@"1234"]; //拼接一个字符串 [str appendString:@"5678"]; //拼接字符串 [str appendFormat:@"age is %i,height is %.2f",24,1.75f]; //替换字符串 NSRange range=[str rangeOfString:@"height"]; [str replaceCharactersInRange:range withString:@"no"]; //插入字符串 [str insertString:@"abc" atIndex:2];
转载于:https://www.cnblogs.com/hqzxbb/p/4402424.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109495.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...