iOS NSString用法大全

1、字符串的初始化

-(void)initStringFun{
  //1、使用Unicode数值数组转化成字符串
  unichar data[6] = {97,98,99,100,101,102};
  NSString * string = [[NSString alloc] initWithCharacters:data length:6];
  NSLog(@"string = %@",string);
  NSString * str = [NSString stringWithCharacters:data length:6];
  NSLog(@"str = %@",str);

 //2、将c风格字符串转NSString对象
 char * cStr = "hello,iOS";
 NSString * cString = [[NSString alloc] initWithUTF8String:cStr];
 NSLog(@"cString = %@",cString);

  //将oc字符串转为c字符串
  const char * strToC = [cString UTF8String];
  NSLog(@"strToC = %s",strToC);

}

2、字符串的常用功能

-(void)commonFunction{
  //1、追加字符串
  NSString * str = @"Hello";
  NSString * book = @",book";
  NSString * strBook = [str stringByAppendingString:book];
  NSLog(@"str = %@",str);
  NSLog(@"strBook = %@",strBook);

  //2、追加带变量的字符串
  NSString * fStr = @"Hello";
  NSString * formatStr = [fStr stringByAppendingFormat:@"%@Book",@"Good"];
  NSLog(@"fStr = %@",fStr);
  NSLog(@"formatStr = %@",formatStr);

  //3、字符串的截取
  NSString * string = @"hello,this is a good book";
  NSString * subStr1 = [string substringToIndex:10];//0-10的子字符串
  NSString * subStr2 = [string substringFromIndex:10];//10-末尾的子字符串
  NSString * subStr3 = [string substringWithRange:NSMakeRange(2, 3)];//2开始的3个字符长度的字符串
  NSLog(@"subStr1 = %@",subStr1);
  NSLog(@"subStr2 = %@",subStr2);
  NSLog(@"subStr3 = %@",subStr3);

  //4、子字符串所在位置及长度
  NSString * mainString = @"hello,world";
  NSString * subStr = @"world";
  NSRange range = [mainString rangeOfString:subStr];
  NSLog(@"range.location = %ld,range,length = %ld",range.location,range.length);

  //5、字符串统一转大小写
  NSString * smallBigStr = @"Hello,World";
  NSString * smallStr = [smallBigStr lowercaseString];
  NSString * bigStr = [smallBigStr uppercaseString];
  NSLog(@"smallStr = %@",smallStr);
  NSLog(@"bigStr = %@",bigStr);

  //6、字符串前后缀
  NSString * preString = @"hello,world";
  BOOL hasPre = [preString hasPrefix:@"hello"];
  NSLog(@"hasPre = %d",hasPre);
  NSString * sufString = @"hello,world";
  [sufString hasSuffix:@"world"];
  NSLog(@"sufString = %d",hasPre);

  //7、字符串长度
  NSString * lengthStr = @"hello,world";
  NSLog(@"lengthStr.length = %ld",lengthStr.length);

  //8、字符串中的字符
  NSString * charStr = @"hello,world";
  unichar c = [charStr characterAtIndex:0];
  NSLog(@"c = %c",c);

  //9、字符串的比较
  NSString * compareStr1 = @"hello";
  NSString * compareStr2 = @"world";
  NSComparisonResult result = [compareStr1 compare:compareStr2];
  NSLog(@"result = %d",result);

  BOOL isEqual = [compareStr1 isEqualToString:compareStr2];
  NSLog(@"isEqual = %d",isEqual);

  //10、字符串的分割
  NSString * sepStr = @"hello-world";
  NSArray * setArray = [sepStr componentsSeparatedByString:@"-"];
  NSLog(@"setArray = %@",setArray);

  NSCharacterSet * charSet = [NSCharacterSet characterSetWithCharactersInString:@"-o"];
  NSArray * array = [sepStr componentsSeparatedByCharactersInSet:charSet];
  NSLog(@"array = %@",array);
}

3、可变字符串

-(void)mutableStringFun{
  NSMutableString * mString = [[NSMutableString alloc] initWithString:@"hello"];
  //1、追加字符串
  [mString appendString:@",book"];
  NSLog(@"mString = %@",mString);

  //2、追加带变量的字符串
  [mString appendFormat:@".%@ Good Book",@"This Is A"];
  NSLog(@"mString = %@",mString);

  //3、删除指定位置字符
  [mString deleteCharactersInRange:NSMakeRange(0, 6)];
  NSLog(@"mString = %@",mString);

  //4、替换指定位置字符
  [mString replaceCharactersInRange:NSMakeRange(0, 6) withString:@"你好"];
 NSLog(@"mString = %@",mString);

  //5、在指定位置插入字符
  [mString insertString:@"你好" atIndex:7];
  NSLog(@"mString = %@",mString);

  //6、覆盖字符串
  [mString setString:@"你好,世界"];
  NSLog(@"mString = %@",mString);


}

4、其它方法

//将int类型 转为16进制的字符串1
-(void)hexStringFromNum{
  int num = 16;
  NSString *hexString = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1x",num]];
  NSLog(@"hexString = %@",hexString);
}

//将int类型 转为16进制的字符串2(待验证)
-(void)longlongToHexStr{
  NSInteger value = 16;
  int len = 2;
  char ASSICCHAR[] = "0123456789ABCDEF";;
  char resultBuffer[len];
  for (int i = 0; i < len; i++) {
      resultBuffer[len - i - 1] = ASSICCHAR[(value >> (i * 4)) & 0x0F];
  }
  NSString * hexString = [NSString stringWithCString:resultBuffer encoding:NSASCIIStringEncoding];
  NSLog(@"hexString = %@",hexString);
}

//字典转为json格式字符串
-(NSString*)jsonStringFromDict{
  NSDictionary * dict = @{@"1":@"test1",
                        @"2":@"test3"};
  NSData * mData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
  NSString * dictString = [[NSString alloc] initWithData:mData encoding:NSUTF8StringEncoding];
 NSLog(@"dictString = %@",dictString);
  return dictString;
}
//将json格式字符串转为字典

-(NSDictionary *)parseJSONStringToNSDictionary{
  NSString * JSONString = [self jsonStringFromDict];
  //将字符串转成二进制数据
  NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
  //利用系统自带的JSON解析,解析成字典格式
  NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
  //将字典返回
  NSLog(@"responseJSON = %@",responseJSON);
  return responseJSON;
}

//去掉非数字字符
-(void)removeUnnumberChar{
  NSString * number = @"156-8888-8888";
  //invertedset:为翻转
  NSCharacterSet *setToRemove = [[ NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
  NSArray * numArray = [number componentsSeparatedByCharactersInSet:setToRemove];
  NSLog(@"numArray = %@",numArray);
  NSString *phoneStr = [numArray componentsJoinedByString:@""];

}


//字符串与int类型互转
-(void)stringToInt{

  //数字转字符串
  int num = 12;
  NSString * intString = [NSString stringWithFormat:@"%d",num];
  NSLog(@"intString = %@",intString);

  //字符串转数字
  NSString * string = @"12";//会转为12;类似@”我“这样的非数字的字符串会转为0
  NSInteger stringInt = [string integerValue];
  NSLog(@"stringInt = %ld",stringInt);

}


//字符串与data互转
-(void)stringToData{
  //把字符串再转变为二进制流
  NSString * string = @"I have a dream";
  NSData * data=[NSMutableData dataWithData:[string dataUsingEncoding:NSUTF8StringEncoding]];
  NSLog(@"data = %@",data);

  //二进制流转变为字符串
  NSString * dataStr=[[NSMutableString alloc] initWithData:data encoding:NSUTF8StringEncoding];
 NSLog(@"dataStr = %@",dataStr);

}

你可能感兴趣的:(iOS NSString用法大全)