温故知新,常用常新
 
//1、创建常量字符串。
NSString *astring = @"This is a String!";
 
//2、创建空字符串,给予赋值。
NSString *astring = [[NSString alloc] init];

astring = @"This is a String!";

[astring release];

NSLog(@"astring:%@",astring);

//

NSString *astring = [[NSString alloc] init];

NSLog(@"0x%.8x", astring);

astring=@"This is a String!";

NSLog(@"0x%.8x", astring);

[astring release];

NSLog(@"astring:%@",astring);
 
//3、在以上方法中,提升速度:initWithString方法
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];

NSLog(@"astring:%@",astring);

[astring release];
 
//4、用标准c创建字符串:initWithCString方法
char *Cstring = "This is a String!";

NSString *astring = [[NSString alloc] initWithCString:Cstring];

NSLog(@"astring:%@",astring);

[astring release];
 
//5、创建格式化字符串:占位符(由一个%加一个字符组成)
int i = 1;

int j = 2;

NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];

NSLog(@"astring:%@",astring);

[astring release];
 
//6、创建临时字符串
NSString *astring;

astring = [NSString stringWithCString:"This is a temporary string"];

NSLog(@"astring:%@",astring);
 

//7、从文件创建字符串

NSString *path = [[NSBundlemainBundle] pathForResource:@"astring.text"ofType:nil];
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];
 

//8、用字符串创建字符串,并写入到文件

NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];

NSLog(@"astring:%@",astring);

NSString *path = @"astring.text";    

[astring writeToFile: path atomically: YES];

[astring release];  

注:此路径path只只是示意,真实路径并非如此

 
//9、用C比较:strcmp函数
char string1[] = "string!";

char string2[] = "string!";

if(strcmp(string1, string2) == 0)
{ NSLog(@"1"); }
 

//10、isEqualToString方法

NSString *astring01 = @"This is a String!";

NSString *astring02 = @"This is a String!";

BOOL result = [astring01 isEqualToString:astring02];

NSLog(@"result:%d",result);
 

//11、compare方法(comparer返回的三种值)

//
NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] == NSOrderedSame; //NSOrderedSame判断两者内容是否相同 NSLog(@"result:%d",result); //
NSString *astring01 = @"This is a String!"; NSString *astring02 = @"this is a String!"; BOOL result = [astring01 compare:astring02] == NSOrderedAscending; //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真) NSLog(@"result:%d",result); //
NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] == NSOrderedDescending; //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) NSLog(@"result:%d",result);
 

//12、不考虑大小写比较字符串

//1.
NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 caseInsensitiveCompare:astring02] == NSOrderedSame; //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) NSLog(@"result:%d",result); //2.
NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame; //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。 NSLog(@"result:%d",result);
 
//13、输出大写或者小写字符串
NSString *string1 = @"A String"; 

NSString *string2 = @"String"; 

NSLog(@"string1:%@",[string1 uppercaseString]);//大写

NSLog(@"string2:%@",[string2 lowercaseString]);//小写

NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小
 

//14、-rangeOfString: //查找字符串某处是否包含其它字符串

NSString *string1 = @"This is a string";

NSString *string2 = @"string";

NSRange range = [string1 rangeOfString:string2];

int location = range.location;

int leight = range.length;

NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];

NSLog(@"astring:%@",astring);

[astring release];
 

//15、-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符

NSString *string1 = @"This is a string";

NSString *string2 = [string1 substringToIndex:3];

NSLog(@"string2:%@",string2);
 

//16、-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符

NSString *string1 = @"This is a string";

NSString *string2 = [string1 substringFromIndex:3];

NSLog(@"string2:%@",string2);
 

//17、-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串

NSString *string1 = @"This is a string";

NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];

NSLog(@"string2:%@",string2);
 

//18、-stringWithCapacity: //按照固定长度生成空字符串

NSMutableString *String;

String = [NSMutableString stringWithCapacity:40];
 

//19、-appendString: and -appendFormat: //把一个字符串接在另一个字符串的末尾

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];

[String1 appendString:@", I will be adding some character"];

[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];

NSLog(@"String1:%@",String1);
 

//20、-insertString: atIndex: //在指定位置插入字符串

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];

[String1 insertString:@"Hi! " atIndex:0];

NSLog(@"String1:%@",String1);
 

//21、-setString:

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];

[String1 setString:@"Hello Word!"];

NSLog(@"String1:%@",String1);
 

//22、-replaceCharactersInRange: withString: //用指定字符串替换字符串中某指定位置、长度的字符串

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];

[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];

NSLog(@"String1:%@",String1);
 

//23、-hasPrefix: //检查字符串是否以另一个字符串开头

NSString *String1 = @"NSStringInformation.txt";

[String1 hasPrefix:@"NSString"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");

[String1 hasSuffix:@".txt"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");
 
//24、扩展路径
NSString *Path = @"~/NSData.txt";

NSString *absolutePath = [Path stringByExpandingTildeInPath];

NSLog(@"absolutePath:%@",absolutePath);

NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
 

//25、文件扩展名

NSString *Path = @"~/NSData.txt";

NSLog(@"Extension:%@",[Path pathExtension]);

(转)NSString 类的使用的更多相关文章

  1. iOS NSString类中获取子字符串

    NSString类中提供了这样三个方法用于获取子字符串: – substringFromIndex://取字符串长度从0开始,当index=str.length时字符串为空"" – ...

  2. [Swift]字符串(String类、NSString类)常用操作

    NS是Cocoa类对象类型的前缀,来源于乔布斯建立的另一家公司--NeXTNSString的使用方法,和Swift语言中的String有很多相似之处. 1.字符串的定义String类 var str1 ...

  3. 替换NSString类中的stringWithFormat:方法

    替换NSString类中的stringWithFormat:方法 先给出源码: YXUseful.h // // YXUseful.h // NSString // // Copyright (c) ...

  4. NSString类里有个hash

    实际编程总会涉及到比较两个字符串的内容,一般会用 [string1 isEqualsToString:string2] 来比较两个字符串是否一致.对于字符串的isEqualsToString方法,需要 ...

  5. NSString 类介绍及用法

    1.NSString常见方法 NSString是 Objective-C 中核心处理字符串的类之一 创建常量字符串,注意使用"@"符号. NSString *astring = @ ...

  6. iOS之07-三大特性之多态 + NSString类

    多态 1.没有继承就没有多态 2.代码体现:父类类型的指针指向子类对象 类的创建: #import <Foundation/Foundation.h> // 动物 @interface A ...

  7. iOS学习13之OC NSString类

    C语言中,字符串是有char(ASC||码)字符组成. OC中,字符串是由unichar(Unicode)字符组成. 1.字符串(NSString) NSString:不可变字符串,即:创建以后,内容 ...

  8. NSString类的相关用法

    一.NSString字符串连接NSString* string; // 结果字符串 NSString* string1, string2; //已存在的字符串 1. string = [NSStrin ...

  9. NSString类

    创建一个字符串 ) NSString *s = @"aaaa"; ) NSString *s1=[NSString new]; s1=@"bbb"; )格式化创 ...

  10. Objective-C:NSString类的常见用法

    几种常见的用法为:字符串的创建.字符串的搜索.字符串的比较.字符串的转换 用途一:字符串的创建 void ex1() { //1.常量字符串的对象 NSString *str1 = @"he ...

随机推荐

  1. Python全栈工程师(每周总结:3)

     ParisGabriel                                                           每天坚持 一天一篇 点个订阅吧  灰常感谢    当个死 ...

  2. 1004 Counting Leaves (30 分)(树的遍历)

    给出一棵树,问每一层各有多少叶子节点 dfs遍历树 #include<bits/stdc++.h> using namespace std; vector<]; int n,m; i ...

  3. java GUI Graphics2D 绘图

    Graphics类提供基本绘图方法,Graphics2D类提供更强大的绘图能力.本节讲解Graphics类,下节讲解Graphics2D. Graphics类提供基本的几何图形绘制方法,主要有:画线段 ...

  4. log4j的各种类的配置

    log4j看上去像是一种简单的,易配置的日志打印技术.但是实际使用的时候发现,还有各种很相似的日志技术.很多地方的配置一乱就不知道怎么对应了.所以应该把log4j的一切做个简单的分类记录. (一)ja ...

  5. [CF954G]Castle Defense

    题目大意:有$n$个点,每个点最开始有$a_i$个弓箭手,在第$i$个位置的弓箭手可以给$[i-r,i+r]$区间加上$1$的防御,你还有$k$个弓箭手,要求你最大化最小防御值 题解:二分答案,从右向 ...

  6. 以太坊源码分析(52)以太坊fast sync算法

    this PR aggregates a lot of small modifications to core, trie, eth and other packages to collectivel ...

  7. 洛谷 P3168 [CQOI2015]任务查询系统 解题报告

    P3168 [CQOI2015]任务查询系统 题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分. 超级计算机中的任务用三元组\((S_i,E_i,P_i) ...

  8. 启动、停止、删除Windows服务

    启动: @echo.服务启动...... @echo off @sc create Service_SMS binPath= "D:\公司制度等文件\项目\河北劳动力市场检测系统\Windo ...

  9. Codeforces 934.D A Determined Cleanup

    D. A Determined Cleanup time limit per test 1 second memory limit per test 256 megabytes input stand ...

  10. lesson 5

    C#中的委托(delegate)与事件(event) 一.委托就是中间人的意思,c#中的委托允许将一个类中的方法传递给另一个能调用该方法的类的某个对象.程序员可以将A类的一个方法m(被包含在某个del ...