//
// main.m
// OC-04_03
//
// Created by Ibokan on 15/12/11.
// Copyright (c) 2015年 ibokan. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//可变字符串 NSMutableString
NSMutableString *string = [NSMutableString stringWithString:@"nihao"];
NSMutableString *string1 = [NSMutableString stringWithFormat:@"hello%d",100];
//从本地读取文件
NSMutableString *string2 = [NSMutableString stringWithContentsOfFile:@"" encoding:NSUTF8StringEncoding error:nil];
//NSMutableString *string3 = [NSMutableString stringWithContentsOfURL:@"" encoding:4 error:nil];
//可变字符串独特初始化方法:初始化一个空间为100的可变字符串
NSMutableString *string4 =[[NSMutableString alloc]initWithCapacity:100];
//c语言的字符串数组转成OC的字符串
//[[NSString alloc]initWithCharacters:<#(const unichar *)#> length:<#(NSUInteger)#>];
NSMutableString *targetString = [NSMutableString stringWithString:@"呵呵,我发现美女了"];
NSRange range = [targetString rangeOfString:@"现"];
NSLog(@"%@",[NSValue valueWithRange:range]);
//替代
[targetString replaceCharactersInRange:range withString:@"*"];
NSLog(@"%@",targetString);
//增加
//在指定位置插入一个字符串
[targetString insertString:@"你是SB么" atIndex:targetString.length];
NSLog(@"%@",targetString);
//在我们的字符串末尾添加字符串
[targetString appendString:@",拜拜!"];
[targetString appendFormat:@"%@,%@",@"你骂逼的",@"草你吗"];
NSLog(@"%@",targetString);
//删除
[targetString deleteCharactersInRange:NSMakeRange(0, 3)];
NSLog(@"%@",targetString);
}
return 0;
}
OC中NSMutableString的使用
最新推荐文章于 2021-04-09 14:58:59 发布