超棒文集Ios开发学习iOS干货

iOS NSString常用操作以及数据转化

2017-03-04  本文已影响603人  逍遥晨旭

一、NSString

1、将NSData转化为NSString

 NSString* str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

2、将NSString 转化为NSData
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;
3、载一个字符串中删除一个字符或字符串

  [_display deleteCharactersInRange:NSMakeRange(index_of_char_to_remove, 1)];

4、从文件读取字符串:initWithContentsOfFile方法

  NSString *path = @"***.text";  
  NSString *astring = [[NSString alloc] initWithContentsOfFile:path];

5、写字符串到文件:writeToFile方法

  NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];  
  NSString *path = @"astring.text";      
  [astring writeToFile: path atomically: YES];

6、比较两个字符串
1>、用C比较:strcmp函数

char string1[] = "string!";  
char string2[] = "string!";  
if(strcmp(string1, string2) = = 0)  {  
    NSLog(@"1");  
}

2>、isEqualToString方法

NSString *astring01 = @"This is a String!";  
NSString *astring02 = @"This is a String!";  
BOOL result = [astring01 isEqualToString:astring02];  
NSLog(@"result:%d",result);

3>、compare方法(comparer返回的三种值)

NSString *astring01 = @"This is a String!";  
NSString *astring02 = @"This is a String!";      
BOOL result = [astring01 compare:astring02] = = NSOrderedSame;      
NSLog(@"result:%d",result);      
//NSOrderedSame判断两者内容是否相同  

7、改变字符串的大小写

NSString *string1 = @"A String";   
NSString *string2 = @"String";   
NSLog(@"string1:%@",[string1 uppercaseString]);//大写  
NSLog(@"string2:%@",[string2 lowercaseString]);//小写  
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小  

8、在字符串中搜索子串

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]];

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

NSString *string1 = @"This is a string";  
NSString *string2 = [string1 substringToIndex:3];  
NSLog(@"string2:%@",string2);  

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

NSString *string1 = @"This is a string";  
NSString *string2 = [string1 substringFromIndex:3];  
NSLog(@"string2:%@",string2);  

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

NSString *string1 = @"This is a string";  
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];  
NSLog(@"string2:%@",string2);

10、获取扩展名:[Path pathExtension]

11、判断是否以某个字符串开始或结尾

     -(BOOL) hasPrefix:(NSString *) aString; //开头
     -(BOOL) hasSuffix:(NSString *) aString; //结尾

二、NSMutableString

1、给字符串分配容量

NSMutableString *String;  
String = [NSMutableString stringWithCapacity:40];  

2、在已有字符串后面添加字符

 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"]];  

3、在已有字符串中按照所给出范围和长度删除字符

  NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  
 [String1 deleteCharactersInRange:NSMakeRange(0, 5)];  

4、在已有字符串后面在所指定的位置中插入给出的字符串

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  
[String1 insertString:@"Hi! " atIndex:0];  

5、将已有的空符串换成其它的字符串

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  
[String1 setString:@"Hello Word!"];  

6、按照所给出的范围,和字符串替换的原有的字符

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];  

7、判断字符串内是否还包含别的字符串(前缀,后缀)

1>、检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString;

NSString *String1 = @"NSStringInformation.txt";  
[String1 hasPrefix:@"NSString"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");  
[String1 hasSuffix:@".txt"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");  

2>、查找字符串某处是否包含其它字符串 - (NSRange) rangeOfString: (NSString *) aString,这一点前面在串中搜索子串用到过;

三、字符串与数组的相互转化

1、从字符串分割到数组

NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];   
NSArray *array = [string componentsSeparatedByString:@","];  

2、从数组合并元素到字符串

NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];  
NSString *string = [array componentsJoinedByString:@","];  

四、字符串比较方法对比

1>、isEqualToString :可以用来比较接收方和当作参数传递来的字符串的内容是否相同,返回yes和no
2>、==:只判断指针数值,而不是它们所指向的内容
3>、compare:比较两个字符串。区分大小写
compare将接收对象和传递来的字符串逐个字符的进行比较,它返回一个NSComparisonResult(枚举类型)来显示结果。

    typedef enum _NSComparisonResult{ 
         NSOrderedAscending=-1; 
         NSOrderedsame; 
         NSOrderedDescending; 
     } NSComparisonResult; 

4>、 compare: options: ( options 是一个位掩码,可以使用|添加选项标记选项 )
options选项:
NSCaseInsensitiveSearch:不区分大小写字符
NSLiteralSearch:进行完全比较,区分大小写
NSNumbericSearch:比较字符串的字符个数,而不是字符值

五、字典转化为字符串

    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&parseError];
    
    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"不,谢了" otherButtonTitles:@"瞅瞅去", nil];
    
    [alert show];

匹配字符串头或尾

hasPrefixe 匹配字符串头
haSuffix 匹配字符串的尾巴

NSString *string1 = @"卡拉是条狗";  
NSString *string2 = @"狗狗叫卡拉";  
//字符串以开头比较  
if([string1 hasPrefix:@"卡拉"])  
{  
      NSLog(@"字符串string1以卡拉开头");  
}  
  //字符串以结尾比较  
if([string2 hasSuffix:@"卡拉"])  
{  
    NSLog(@"string2字符串以卡拉结尾");  
}
上一篇下一篇

猜你喜欢

热点阅读