main.c
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
{
//用字符串字面量创建字符串对象
NSString *str1 = @"hello world";
//用已有的字符串对象创建字符串对象
NSString *str2 = [NSString stringWithString:str1];
//用C得字符指针创建字符串对象
NSString *str3 = [NSString stringWithUTF8String:"I am a girl"];
int a = 3,b=4;
NSString *str4 = [NSString stringWithFormat:@"我爱你%d+%d= %d",a,b,a+b];
// 将str写到a.txt文本里去
[str4 writeToFile:@"/Users/IOS1601/desktop/a.txt" atomically:YES
encoding:NSUTF8StringEncoding error:nil];
//通过统一资源定位符获取网络资源创建字符串对象
NSString *str5 = [NSString stringWithContentsOfURL:[NSURL
URLWithString:@"http://www.baidu.com"] encoding:NSUTF8StringEncoding error:nil];
NSString *str6 = [NSString stringWithContentsOfFile:@"/Users/Hao/Desktop/a.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", str1);
NSLog(@"%@", str2);
NSLog(@"%@", str3);
NSLog(@"%@", str4);
NSLog(@"%@", str5);
NSLog(@"%@",[str1 uppercaseString]);//将str1转为大写
NSLog(@"%@",[str2 lowercaseString]);//转为小写
NSString *str7 = @"我们正在学习OC";
unichar ch = [str7 characterAtIndex:3];
NSLog(@"%C",ch);
NSString *str8 = @"我爱你母亲";
str7 = [str7 stringByAppendingString:str8];
NSLog(@"%@",str7);
NSRange range= {
2,3};NSString *str9 = [str8 substringWithRange:range];
NSLog(@"%@",str9);
printf("");
NSString *str = @"欢迎来到鹿微微鹿的OC博客~~ ";
while (1) {
printf("%s\n\n\n",[str UTF8String]);
str = [[str substringFromIndex:1] stringByAppendingString:
[str substringToIndex:1]];
//这一段的意思是截取字符串的一部分,又截取后面的一部分添加到前面部分的后面,无限循环下去就形成
//一个跑马灯的感觉
usleep(100000);
}
}
return 0;
}
运行结果如下!!