将string字符串转换为array数组
NSArray *array = [Str componentsSeparatedByString:@","];
将array数组转换为string字符串
NSString *tempString = [mutableArray componentsJoinedByString:@","];--分隔符
//替换 如: 1234****5678
-(NSString *)changeTelePoneNumenber:(NSString*)str
{
if (str && ![str isEqualToString:@""]) {
str = [str stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
}
return str;
}
//将123 从 123456中 替换或删除
NSString *str1 = @"123456";
NSString *str2 = @"123";
string = [str1 stringByReplacingOccurrencesOfString:str2 withString:@""];
NSLog(@"~~~~ =%@", str1);//456
//判断str 是否包含有 qwe
NSString *str = @"qwer";
if([Label.text rangeOfString:@"qwe"].location != NSNotFound) {
NSLog(@"yes");
} else{
NSLog(@"no");
}
//在指定下标插入字符串
NSMutableString *str1=[[NSMutableStringalloc]initWithString:@"hello"];//存在堆区,可变字符串
[str1 insertString:@"str" atIndex:3];
// 删除范围内的字符串
// - (void)deleteCharactersInRange:(NSRange)range;
// 拼接字符串
// stringByAppendingString
类型转换
NSString *tempA = @"123";
NSString *tempB = @"456";
// 1,字符串拼接
NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];
//2,字符转NSInteger NSInteger k = [newString integerValue];
//2,NSInteger转字符 NSString *s = [NSString stringWithFormat:@"%ld",k];
//2,字符转int int intString = [newString intValue];
//3,int转字符 NSString *stringInt = [NSString stringWithFormat:@"%d",intString];
//4,字符转float float floatString = [newString floatValue];
//5,float转字符 NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];
NSNumber怎么转换成NSString
或者
NSNumber *
NSString *String = number.description;
#pragma mark - 比较字符串
NSString *str1 = @"basb";
NSString *str2 = @"adfsd";
BOOL result = [str1 compare:str2];
NSLog(@"比较结果==%d", result);
返回值:
typedef enum _NSComparisonResult { NSOrderedAscending = -1, // < 升序
NSOrderedSame, // = 等于
NSOrderedDescending // > 降序
} NSComparisonResult;
例子:版本号比较
NSString *num1 = @"5.2.0";
NSString *num2 = @"5.3.0";
if ([num1 compare:num2 options:NSNumericSearch] == NSOrderedDescending)
{
ULog(@"%@ is bigger",num1);
}else {
ULog(@"%@ is bigger",num2);
}
解释:
NSOrderedDescending是降序,如果numb1>numb2用这个函数相比较那么就等于降序。
#pragma mark - 获取字符串
截取字符串
NSString *str1 = @"123456qwer";
// 1//第几位到最后一位
NSString *str2 = [str1 substringFromIndex:1];
NSLog(@"%@", str2);//23456qwer
// 1//第0位(开始)到 哪一位
NSString *str3 = [str1 substringToIndex:3];
NSLog(@"%@", str3);//123
// 2
NSString *str1 = @"1234567";
NSRange range = NSMakeRange(2, 3);
NSString *str4 = [str1 substringWithRange:range];
NSLog(@"%@", str4);//345 打印
#pragma mark - 以某个字符开头或者结尾
NSString *str1 = @“12345";
// 判断字符串是否以什么字符开头
if ([str1 hasPrefix:@“45"]) {
NSLog(@"有");
}else{
NSLog(@"无");
}
// 判断字符串是否以什么字符后缀
if ([str1 hasSuffix:@“12"]) {
NSLog(@"有");
}else{
NSLog(@"无");
}
#pragma mark - 大小写更换
NSString *str = @"BBBBfdfdsds";
// 大写变小写
str = [str uppercaseString];
// 小写变大写
str = [str lowercaseString];
NSLog(@"%@", str);
////邮箱
+ (BOOL) validateEmail:(NSString *)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
//判断字符串是否为纯数字
-(BOOL)ispureInt:(NSString*)string
{
NSScanner *scan = [NSScanner scannerWithString:string];
int val;
return [scan scanInt:&val]&&[scan isAtEnd];
}
判断字符串是否为纯数字
明杰//MJExtension.h
if ([self. phoneTxetField.text mj_isPureInt]) {
}
// 给数组 排序
[self.allKeysArray sortUsingSelector:@selector(compare:)];
//
-(NSString *)textFieldText:(NSString *)text Str:(NSString*)str
{
if ([text isEqualToString:@""]) {
return @"";
}
if (![str isEqualToString:@""]) {
return [NSString stringWithFormat:@"%@%@", text, str];
}else{
//根据起始位置和范围获得字符串(包括起始位置)
NSRange range = NSMakeRange(0, str.length - 1);
NSString *string = [str substringWithRange:range];
return string;
}
}
http://www.jianshu.com/p/0180c4a765e5 正则
https://www.zhihu.com/question/20350598 键盘类型
@"[a-zA-Z0-9]*" 0-9 a-z A-Z 个人认为 * 是可以删除 不然输入了之后无法删除
@"[a-zA-Z0-9@.]*" 0-9 a-z A-Z @ . 个人认为 * 是可以删除 不然输入了之后无法删除
@"[\u4e00-\u9fa5]+" 中文
//输入的是中文
- (BOOL)inputShouldChinese:(NSString*)string
{
NSString *regex = @"[\u4e00-\u9fa5]+”;//中文
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if ([pred evaluateWithObject:string]) {
return YES;
}
return NO;
}
//判断 整数 小数(两位小数) 格式
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
BOOL isHaveDian = YES;
if ([textField.text rangeOfString:@"."].location == NSNotFound){
isHaveDian =NO;
}
if ([string length] > 0){
unichar single = [string characterAtIndex:0];//当前输入的字符
if ((single >= '0' && single <= '9') || single == '.'){//数据格式正确
//首字母不能为0和小数点
if([textField.text length] == 0){
if(single == '.'){
[[HudManager manager] showHudWithText:@"亲,第一个数字不能为小数点!"];
[textField.text stringByReplacingCharactersInRange:range withString:@""];
return NO;
}
if (single == '0'){
[[HudManager manager] showHudWithText:@"亲,第一个数字不能为小数点!"];
[textField.text stringByReplacingCharactersInRange:range withString:@""];
return NO;
}
}
//输入的字符是否是小数点
if (single == '.'){
if(!isHaveDian){//text中还没有小数点
isHaveDian =YES;
return YES;
}else{
[[HudManager manager] showHudWithText:@"亲,您已经输入过小数点了!"];
[textField.text stringByReplacingCharactersInRange:range withString:@""];
return NO;
}
}else{
if (isHaveDian) {//存在小数点
//判断小数点的位数
NSRange ran = [textField.text rangeOfString:@"."];
if (range.location - ran.location <= 2) {
return YES;
}else{
[[HudManager manager] showHudWithText:@"亲,您最多输入两位小数!"];
return NO;
}
}else{
return YES;
}
}
}else{//输入的数据格式不正确
[[HudManager manager] showHudWithText:@"亲,您输入的格式不正确!"];
[textField.text stringByReplacingCharactersInRange:range withString:@""];
return NO;
}
}else{
return YES;
}
}
字典转json字符串
NSData *data=[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
获取的json字符
NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
json字符串转字典
NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
if(err) {
NSLog(@"json解析失败:%@",err);
}
NSLog(@"dic----%@", dic);
for (NSString *key in dic) {
NSLog(@"key===%@", key);
NSLog(@"value===%@", dic[key]);
}
//根据字符串生成类class
[vc isKindOfClass:[NSClassFromString(@"AProductSearchViewController") class]]
label.attributedText = [self changeTextColcrWithTitleStr:@"姓名*:" Str:@"*"];
-(NSAttributedString *)changeTextColcrWithTitleStr:(NSString *)title Str:(NSString *)str
{
// 设置标签文字
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString: title];
// 获取标红的位置和长度
NSRange range = [title rangeOfString:str]; //获取需要变量文字的文字
// 设置标签文字的属性
[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:20.0]} range:range];
// titleLable.attributedText = attrituteString;
return attrituteString;
}
字符串操作 关键字高亮
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...