#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//字符串的常见方法
NSString *str = @"ilovechina";
//转换成大写
str = [str uppercaseString];
NSLog(@"%@",str);
//转换成小写
str = [str lowercaseString];
NSLog(@"%@",str);
//比较两个字符串是否一样
NSString *stemp = @"ilovechani";
BOOL isSame = [str isEqualToString:stemp];
NSLog(@"%d",isSame);
//截取一段字符串
NSRange range = [str rangeOfString:@"love"];
NSString *subStr = [str substringFromIndex:range.location];//从指定下标处开始截取
NSLog(@"%@",subStr);
subStr = [str substringWithRange:range];//从指定范围处开始截取
NSLog(@"%@",subStr);
//比较两个字符串的大小
}
return 0;
}
void printNSString(){
//字符串的输出方式
NSString *test = @"我希望输出到一个文件中!";
NSURL *url = [NSURL URLWithString:@"/Users/WangJie/Desktop/Foundation/test1.txt" ];
[test writeToFile:@"/Users/WangJie/Desktop/Foundation/test1.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
[test writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
/*
字符串创建的5种方式
*/
void initNSString(){
//1、最简单
NSString *str1 = @"我是一个字符串";
NSLog(@"%@",str1);
//2、动态创建
NSString *str2 = [[NSString alloc] initWithFormat:@"字符串的第%d种形式",2];
str2 = [NSString stringWithFormat:@"字符串的第%d种形式",2];
NSLog(@"%@",str2);
//3、通过C语言字符串创建,将C语言字符串,转换成OC字符串
NSString *str3 = [[NSString alloc] initWithUTF8String:"字符串的第三种形式"];
str3 = [NSString stringWithUTF8String:"字符串的第三种形式"];
NSLog(@"%@",str3);
//4、从文件中读取创建:第一个参数:文件的绝对路径 第二个参数:表示字符编码:GBK2312 UTF-8
NSString *str4 = [NSString stringWithContentsOfFile:@"/Users/WangJie/Desktop/OC第一天/01.NSLog练习.m" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str4);
//5、从URL读取;
NSURL *urlBD = [[NSURL alloc] initWithString:@"http://www.baidu.com"];
NSString *str5 = [[NSString alloc] initWithContentsOfURL:urlBD encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str5);
}
|
|