How to insert a character into a NSString

How do I insert a space to a NSString.

I need to add a space at index 5 into:

NString * dir = @"abcdefghijklmno";

To get this result:

abcde fghijklmno

with:

NSLOG (@"%@", dir);


You need to use NSMutableString
NSMutableString *mu = [NSMutableString stringWithString:dir];
[mu insertString:@" " atIndex:5];

or you could use those method to split your string :

– substringFromIndex:
– substringWithRange:
– substringToIndex: 

and recombine them after with

– stringByAppendingFormat:
– stringByAppendingString:
– stringByPaddingToLength:withString:startingAtIndex: 

http://stackoverflow.com/questions/8722051/how-to-insert-a-character-into-a-nsstring

How to insert a character into a NSString,布布扣,bubuko.com

时间: 2024-11-04 23:19:42

How to insert a character into a NSString的相关文章

IOS中NSString的常见用法

iOS NSString的常用用法 //1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; //3.在以上方法中,提升速度:initWithString方法 NSString *astring = [[NSString allo

NSString 常用方法

//1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; [astring release]; NSLog(@"astring:%@",astring); // NSString *astring = [[NS

NSString常用方法

//一.NSString //1.创建常量字符串.NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; [astring release]; NSLog(@"astring:%@",astring);// NSString *ast

NSString 用法大全。

一.NSString 创建字符串.  NSString *astring = @"This is a String!"; 创建空字符串,给予赋值.  NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; NSLog(@"astring:%@",astring); string release]; 使用变量初始化  NSString *name = 

NSString、NSMutableString基本用法

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 游戏官方下载:http://dwz.cn/RwTjl 游戏视频预览:http://dwz.cn/RzHHd 游戏开发博客:http://dwz.cn/RzJzI 游戏源码传送:http://dwz.cn/Nret1 NSString其实是一个对象类型.NSString是NSObject(Cocoa Founda

OC语言--字符串(NSString& NSMutableString)

首先声明一下: OC 中的函数,是全局的,如 NSLog():,它不属于任何类,不属于任何一个对象. OC 中的方法,非全局的,init 方法,用中括号来使用,它属于类本身,或者属于对象本身:分为类方法,和实例方法(对象方法):方法被封装对象,或类模板当中 1. 不可变字符串NSString 1.1 OC字符串和C字符串的不同 OC字符串是对象,占用堆空间:C 字符串是数据,它占用栈空间 1.2 OC字符串的常见操作 (1) 常用创建方法 //实例方法创建字符串 - (instancetype)

iOS NSString的常用用法

//1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; [astring release]; NSLog(@"astring:%@",astring); // NSString *astring = [[NS

Objective-C NSString的常用用法

//1.创建常量字符串. NSString *astring = @"This is a String!";   //2.创建空字符串,给予赋值. NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; [astring release]; NSLog(@"astring:%@",astring);//NSString *astring = [[NS

<<黑马程序员>>NSString 和 NSMutableString 的使用

NSString.NSMutableString基本用法 NSString其实是一个对象类型.NSString是NSObject的子类 一.NSString的创建 1.创建常量字符串.NSString *astring = @"This is a String!"; 2.创建空字符串,给予赋值.NSString *astring = [[NSString alloc] init];astring = @"This is a String!";[astring rel