// // main.m // String // // Created by neworigin on 14-9-17. // Copyright (c) 2014年 Damon. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //一、NSString /*---------------- 创建字符串的方法 ----------------*/ //1、创建常量字符串。 NSString *astring1 = @"This is a String!"; NSLog(@"astring1:%@",astring1); //2、创建空字符串,给予赋值。 NSString *astring2 = [[NSString alloc] init]; astring2 = @"This is a String!"; [astring2 release]; NSLog(@"astring2:%@",astring2); //3、在以上方法中,提升速度:initWithString方法 NSString *astring3 = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring3:%@",astring3); [astring3 release]; //4、用标准c创建字符串:initWithCString方法 char *Cstring4 = "This is a String!"; NSString *astring4 = [[NSString alloc]initWithCString:Cstring4 encoding:NSUTF8StringEncoding]; NSLog(@"astring4:%@",astring4); [astring4 release]; //5、创建格式化字符串:占位符(由一个%加一个字符组成) int i = 1; int j = 2; NSString *astring5 = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]]; NSLog(@"astring5:%@",astring5); [astring5 release]; //6、创建临时字符串 NSString *astring6; astring6 = [NSString stringWithCString:"This is a temporary string" encoding:NSUTF8StringEncoding]; NSLog(@"astring6:%@",astring6); /*----------写字符串到文件:writeToFile方法-----------*/ NSString *astring7 = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring7:%@",astring7); NSString *path1 = @"/Users/neworigin/Desktop/txt1.txt"; [astring7 writeToFile:path1 atomically:YES encoding:NSUTF8StringEncoding error:nil]; [astring7 release]; /*----从文件读取字符串:initWithContentsOfFile方法----*/ NSString *path2 = @"/Users/neworigin/Desktop/txt1.txt"; NSString *astring8 = [[NSString alloc]initWithContentsOfFile:path2 encoding:NSUTF8StringEncoding error:nil]; NSLog(@"astring8:%@",astring8); [astring7 release]; /*----------------比较两个字符串----------------*/ //用C比较:strcmp函数 char string1[] = "string!"; char string2[] = "string!"; if(strcmp(string1, string2) == 0) { NSLog(@"1"); } //isEqualToString方法 NSString *astring9 = @"This is a String!"; NSString *astring10 = @"This is a String!"; BOOL result = [astring9 isEqualToString:astring10]; NSLog(@"result:%d",result); //compare方法(comparer返回的三种值) NSString *astring11 = @"This is a String!"; NSString *astring12 = @"This is a String!!"; NSComparisonResult result2 = [astring11 compare:astring12]; NSLog(@"result2:%ld",result2); //NSCaseInsensitiveSearch:不区分大小写比较 // NSLiteralSearch:进行完全比较,区分大小写 //NSNumericSearch:比较字符串的字符个数,而不是字符值。 /*-----------改变字符串的大小写------------*/ NSString *string30 = @"A String"; NSString *string31 = @"sTring"; NSLog(@"string30:%@",[string30 uppercaseString]);//大写 NSLog(@"string31:%@",[string31 lowercaseString]);//小写 NSLog(@"string31:%@",[string31 capitalizedString]);//首字母大小 /*----------------在串中搜索子串----------------*/ NSString *string13 = @"This is a string"; NSString *string14 = @"string"; NSRange range = [string13 rangeOfString:string14]; NSUInteger location = range.location; NSUInteger leight = range.length; NSString *astring15 = [[NSString alloc] initWithFormat:@"Location:%lu,Leight:%lu",(unsigned long)location,(unsigned long)leight]; NSLog(@"astring15:%@",astring15); [astring15 release]; /*----------------抽取子串 ----------------*/ //-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符 NSString *string16 = @"This is a string"; NSString *string17 =[string16 substringToIndex:3]; NSLog(@"string17:%@",string17); //-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符 NSString *string18 = @"This is a string"; NSString *string19 = [string18 substringFromIndex:3]; NSLog(@"string19:%@",string19); //-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串 NSString *string20 = @"This is a string"; NSString *string21 = [string20 substringWithRange:NSMakeRange(0, 4)]; NSLog(@"string21:%@",string21); //扩展路径 NSString *Path3 = @"/Users/neworigin/Desktop/txt1.txt"; NSString *absolutePath = [Path3 stringByExpandingTildeInPath]; NSLog(@"absolutePath:%@",absolutePath); NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]); //文件扩展名 NSString *Path4 = @"/Users/neworigin/Desktop/txt1.txt"; NSLog(@"Extension:%@",[Path4 pathExtension]); return 0; } }
版权属于:东哥笔记 - DongGe.org
本文链接:https://dongge.org/blog/36.html
自2017年12月26日起,『转载以及大段采集进行后续编辑』须注明本文标题和链接!否则禁止所有转载和采集行为!
4 条评论
I have been browsing online more than three hours today for window painting calgary & NSString的用法总结,
yet I never found any interesting article like yours. It is pretty worth enough for me.
In my view, if all webmasters and bloggers made good content as you did, the internet will be much
more useful than ever before.
I have been reviewin online more than 5 hours today for interior
painting deals & NSString的用法总结, yet I never found any
interesting article like yours. It is pretty worth enough for me.
Personally, if all web owners and bloggers made good content as you did,
the web will be much more useful than ever before.
I've been reviewing online more than three hours today for calgary
interior painting & NSString的用法总结, yet
I never found any interesting article like yours. It's pretty worth enough for me.
In my opinion, if all website owners and bloggers made good content as you did, the
internet will be a lot more useful than ever before.
thank you