Objective-C 常用字符串操作
2015-06-07 00:00
417 查看
// // main.m // KenshinCui // // Created by hxd_mac on 15-6-7. // Copyright (c) 2015年 hxd198. All rights reserved. // #import <Foundation/Foundation.h> //字符串初始化 void Test1() { char *str1 = "C String"; NSString *str2 = @"OC string"; NSString *str3 = [[NSString alloc] init]; str3 =@"oc string"; NSString *str4 = [[NSString alloc] initWithString:@"Objectiv-C string" ]; NSString *str5 = [[NSString alloc] initWithFormat:@"age is %i, name is %@", 26, @"str5"]; NSString *str6 = [[NSString alloc] initWithUTF8String:str1]; NSString *str7 = [NSString stringWithFormat:@"objective-c string"]; NSLog(@"str1 = %s, str2 = %@, str3 = %@, str4 = %@, str5 = %@, str6 = %@, str7 = %@", str1, str2, str3, str4, str5, str6, str7); } //字符串大小写转换和比较 void Test2() { NSLog(@"\"hello world\" to upper is %@",[@"hello world" uppercaseString]); NSLog(@"\"hello world\" to lower is %@", [@"hello world" lowercaseString]); NSLog(@"\"hello world\" to capitalize is %@", [@"hello world" capitalizedString]); BOOL result = [@"abc" isEqualToString:@"Abc"]; NSLog(@"%d", result); NSComparisonResult result2 = [@"abc" compare:@"aBC"]; if (result2 == NSOrderedAscending) { NSLog(@"left < right"); } else if (result2 == NSOrderedDescending) { NSLog(@"left > right"); } else { NSLog(@"the same"); } } //字符串查询 void Test3() { NSLog(@"has preifx %i", [@"sun of shine" hasPrefix:@"sun"]); NSLog(@"has suffix %i", [@"abcdefg" hasSuffix:@"fg"]); NSRange range = [@"abcedefgh" rangeOfString:@"ced"]; if (range.location ==NSNotFound) { NSLog(@"not found"); } else { NSLog(@"range is %@", NSStringFromRange(range)); } } //字符串分割合并 void Test4() { NSLog(@"%@", [@"abcdefgh" substringFromIndex:3]); NSLog(@"%@", [@"abcdefgh" substringToIndex:3]); NSLog(@"%@", [@"abcdefgh" substringWithRange:NSMakeRange(3, 2)]); NSString *string = @"abc.123.mp3"; NSLog(@"%@", [string componentsSeparatedByString:@"."]); NSLog(@"%@", [[string componentsSeparatedByString:@"."] componentsJoinedByString:@":"]); } //其它操作 void Test5() { NSLog(@"%i", [@"12" intValue]); NSLog(@"%zi", [@"深圳你好!" length]); NSLog(@"%c", [@"abc" characterAtIndex:0]); //转换成c语言字符串 const char *string = [@"123" UTF8String]; NSLog(@"%s", string); } int main(int argc, const char * argv[]) { @autoreleasepool { Test1(); Test2(); Test3(); Test4(); Test5(); } return 0; } 输出结果: 2015-06-07 15:11:47.030 KenshinCui[1369:303] str1 = C String, str2 = OC string, str3 = oc string, str4 = Objectiv-C string, str5 = age is 26, name is str5, str6 = C String, str7 = objective-c string 2015-06-07 15:11:47.058 KenshinCui[1369:303] "hello world" to upper is HELLO WORLD 2015-06-07 15:11:47.060 KenshinCui[1369:303] "hello world" to lower is hello world 2015-06-07 15:11:47.062 KenshinCui[1369:303] "hello world" to capitalize is Hello World 2015-06-07 15:11:47.063 KenshinCui[1369:303] 0 2015-06-07 15:11:47.065 KenshinCui[1369:303] left > right 2015-06-07 15:11:47.066 KenshinCui[1369:303] has preifx 1 2015-06-07 15:11:47.067 KenshinCui[1369:303] has suffix 1 2015-06-07 15:11:47.069 KenshinCui[1369:303] range is {2, 3} 2015-06-07 15:11:47.070 KenshinCui[1369:303] defgh 2015-06-07 15:11:47.072 KenshinCui[1369:303] abc 2015-06-07 15:11:47.074 KenshinCui[1369:303] de 2015-06-07 15:11:47.075 KenshinCui[1369:303] ( abc, 123, mp3 ) 2015-06-07 15:11:47.076 KenshinCui[1369:303] abc:123:mp3 2015-06-07 15:11:47.077 KenshinCui[1369:303] 12 2015-06-07 15:11:47.152 KenshinCui[1369:303] 5 2015-06-07 15:11:47.154 KenshinCui[1369:303] a 2015-06-07 15:11:47.155 KenshinCui[1369:303] 123
相关文章推荐
- Objective-C 文件操作
- [C++/object c]_[初级]_[std::map容器的使用总结和NSDictionary词典使用总结]
- 使用[NSKeyedUnarchiver unarchiveObjectWithData:data]程序crash
- 基于json-lib.jar包 创建JSONObject的四个常用方法
- ComFailException:can't co-create object.错误的猜测的一种解决方法
- WaitForMultipleObjects使用详解
- 十分钟让你明白Objective-C的语法(和Java、C++的对比)
- Different ways to create an object in Java
- 浏览器对象模型(Browser Object Model)
- ArcGIS操作——object name is not a regular identifier for underlaying DBMS[ArcGIS企业级数据导入错误]
- 轻松学习之一--Objective-C消息转发
- Object.prototype.toString.call() 区分对象类型
- Objective-C的入门(一)
- Object-c分类和协议
- Objective-c的内存管理MRC与ARC
- Delphi 中的 procedure of object .
- object-c面向对象1
- Objective-C 2.0属性(Property) retain,copy,assign,copy
- NSObject和反射
- java中Array/List/Map/Object与Json互相转换详解