字符串的基本应用

NSString * string = @“11111111111”;



NSString * str1 = [string substringToIndex:3];

NSLog(@"%@",str1);

专门截取字符串的一块从第3位开始,长度为4

NSString * str2 = [string substringWithRange:NSMakeRange(3, 4)];

NSLog(@"%@",str2);

从第七位开始带结束

NSString * str3 = [string substringFromIndex:7];

NSLog(@"%@",str3);



string = [NSString stringWithFormat:@"%@-%@-%@",str1,str2,str3];

NSString *tempA = @"123";

NSString *tempB = @"456";

1,字符串拼接

NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];

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



将string字符串转换为array数组

NSArray  *array = [Str componentsSeparatedByString:@","];

==反向方法

将array数组转换为string字符串

NSString *tempString = [mutableArray componentsJoinedByString:@","];--分隔符



判断字符串是否包含字符

BOOL is =  [string containsString:@"%2520"];



/// 替换字符,将字符串中的部分数据换成另一个

/// @param string 要修改的字符串

/// @param num 替换的起始位置

/// @param end 替换的长度

-(NSString *)string:(NSMutableString *)string start:(NSInteger)num end:(NSInteger)end{



    NSRangerange =NSMakeRange(num, end);



    NSString* str2  =@"";

    for(inti =0; i < end; i++) {

        str2 = [NSStringstringWithFormat:@"%@*",str2];

    }



    [stringreplaceCharactersInRange:range withString:str2];



    returnstring;

}
[string  stringByReplacingOccurrencesOfString:@"你" withString:@"他"]//将字符串中的部分文字替换成其他文字

你可能感兴趣的:(字符串的基本应用)