每日一练: OC中的NSString及常用的几个方法
2018-03-10 23:08
337 查看
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here...
//直接赋值 NSString *myCountry = @"My Country is China"; NSLog(@"The sentence is %@ \n", myCountry);
//赋值使用特定的格式 NSDate *now = [NSDate date]; NSString *theDateNow = [NSString stringWithFormat:@"The time is %@", now]; NSLog(@"Now the string is %@", theDateNow); //NSString 一些常用方法 NSString *orignalString = @"This is an Apple"; //Check the length NSUInteger stringlen = [orignalString length]; NSLog(@"The String length is %lu \n", stringlen); //Change to Upper Cases NSString *upperString = [orignalString uppercaseString]; NSLog(@"Now the String is: %@ \n", upperString); NSString *secondString = @"This is an apple"; if ([orignalString isEqualToString:secondString]) { NSLog(@"The two Strings are same"); } else { NSLog(@"The two Strings are not same"); } NSString *string = @"Hi,boy."; NSLog(@"My string is %@ \n", string); NSString *thirdString = [[NSString alloc] initWithString:string]; NSLog(@"The third String is %@ \n", thirdString); //(unichar)characterAtIndex:(NSUInteger)index; unsigned short achar = [thirdString characterAtIndex:2]; NSLog(@"The second char is %hu \n",achar); //to check the method-hasPrefix if([string hasPrefix:@"Him"]){ NSLog(@"Yes, it has Him"); }else{ NSLog(@"No, it does not have a him"); } //convert the string to int NSString *stringWithNum = @"234"; int stringNum = [stringWithNum intValue]; NSLog(@"The value for stringNum is %d \n", stringNum); //string append new string NSString *forthString = [thirdString stringByAppendingString:@" I Love OC"]; NSLog(@"The forth string is %@", forthString); } return 0;}
Result:
2018-03-10 23:07:18.558085+0800 TOCObjectivea[56935:13868635] The sentence is My Country is China2018-03-10 23:07:18.561513+0800 TOCObjectivea[56935:13868635] Now the string is The time is 2018-03-10 15:07:18 +00002018-03-10 23:07:18.561571+0800 TOCObjectivea[56935:13868635] The String length is 162018-03-10 23:07:18.561678+0800 TOCObjectivea[56935:13868635] Now the String is: THIS IS AN APPLE2018-03-10 23:07:18.561709+0800 TOCObjectivea[56935:13868635] The two Strings are not same2018-03-10 23:07:18.561727+0800 TOCObjectivea[56935:13868635] My string is Hi,boy.2018-03-10 23:07:18.561748+0800 TOCObjectivea[56935:13868635] The third String is Hi,boy.2018-03-10 23:07:18.561764+0800 TOCObjectivea[56935:13868635] The second char is 442018-03-10 23:07:18.561778+0800 TOCObjectivea[56935:13868635] No, it does not have a him2018-03-10 23:07:18.561840+0800 TOCObjectivea[56935:13868635] The value for stringNum is 2342018-03-10 23:07:18.561985+0800 TOCObjectivea[56935:13868635] The forth string is Hi,boy. I Love OCProgram ended with exit code: 0
int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here...
//直接赋值 NSString *myCountry = @"My Country is China"; NSLog(@"The sentence is %@ \n", myCountry);
//赋值使用特定的格式 NSDate *now = [NSDate date]; NSString *theDateNow = [NSString stringWithFormat:@"The time is %@", now]; NSLog(@"Now the string is %@", theDateNow); //NSString 一些常用方法 NSString *orignalString = @"This is an Apple"; //Check the length NSUInteger stringlen = [orignalString length]; NSLog(@"The String length is %lu \n", stringlen); //Change to Upper Cases NSString *upperString = [orignalString uppercaseString]; NSLog(@"Now the String is: %@ \n", upperString); NSString *secondString = @"This is an apple"; if ([orignalString isEqualToString:secondString]) { NSLog(@"The two Strings are same"); } else { NSLog(@"The two Strings are not same"); } NSString *string = @"Hi,boy."; NSLog(@"My string is %@ \n", string); NSString *thirdString = [[NSString alloc] initWithString:string]; NSLog(@"The third String is %@ \n", thirdString); //(unichar)characterAtIndex:(NSUInteger)index; unsigned short achar = [thirdString characterAtIndex:2]; NSLog(@"The second char is %hu \n",achar); //to check the method-hasPrefix if([string hasPrefix:@"Him"]){ NSLog(@"Yes, it has Him"); }else{ NSLog(@"No, it does not have a him"); } //convert the string to int NSString *stringWithNum = @"234"; int stringNum = [stringWithNum intValue]; NSLog(@"The value for stringNum is %d \n", stringNum); //string append new string NSString *forthString = [thirdString stringByAppendingString:@" I Love OC"]; NSLog(@"The forth string is %@", forthString); } return 0;}
Result:
2018-03-10 23:07:18.558085+0800 TOCObjectivea[56935:13868635] The sentence is My Country is China2018-03-10 23:07:18.561513+0800 TOCObjectivea[56935:13868635] Now the string is The time is 2018-03-10 15:07:18 +00002018-03-10 23:07:18.561571+0800 TOCObjectivea[56935:13868635] The String length is 162018-03-10 23:07:18.561678+0800 TOCObjectivea[56935:13868635] Now the String is: THIS IS AN APPLE2018-03-10 23:07:18.561709+0800 TOCObjectivea[56935:13868635] The two Strings are not same2018-03-10 23:07:18.561727+0800 TOCObjectivea[56935:13868635] My string is Hi,boy.2018-03-10 23:07:18.561748+0800 TOCObjectivea[56935:13868635] The third String is Hi,boy.2018-03-10 23:07:18.561764+0800 TOCObjectivea[56935:13868635] The second char is 442018-03-10 23:07:18.561778+0800 TOCObjectivea[56935:13868635] No, it does not have a him2018-03-10 23:07:18.561840+0800 TOCObjectivea[56935:13868635] The value for stringNum is 2342018-03-10 23:07:18.561985+0800 TOCObjectivea[56935:13868635] The forth string is Hi,boy. I Love OCProgram ended with exit code: 0
相关文章推荐
- 每日一练:OC中的关于NSString常用的几个方法
- 【Objective-C】OC中字符串(NSString)的基本概念和常用处理方法
- OC中NSString 的常用方法 (转载)
- iOS开发-Day17-OC NSString / NSMutableString常用方法
- 【Objective-C】OC中字符串(NSString)的基本概念和常用处理方法
- OC NSString 的一些常用方法
- OC学习 第五章 NSString和NSMutablestring的一些常用方法
- OC中NSString 的常用方法
- 爬爬爬之路:OC语言(四) NSString NSSArray NSValue常用方法介绍
- oc-Foundation框架-NSString-常用排序方法
- oc-Foundation框架-NSString-常用方法
- OC NSString 常用方法
- iOS-OC-基础-NSString常用方法
- OC中NSString 的常用方法
- OC中字符串类(NSString和NSMutableString)常用方法汇总
- OC中NSString 的常用方法
- OC_NSString的常用方法
- OC中NSString 的常用方法
- Objective-C中数组对象排序的方法(OC中NSString的常用API的基础应用2)
- NSString的几个常用的方法