替换Objective-C中字符串中的一个字符 [英] Replacing one character in a string in Objective-C

查看:108
本文介绍了替换Objective-C中字符串中的一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人可以帮助我 - 我想替换字符串中的某个字符,我想知道最好的方法是什么?

Hoping somebody can help me out - I would like to replace a certain character in a string and am wondering what is the best way to do this?

我知道字符的位置,例如,如果我想将字符串中的第3个字符从A更改为B - 我将如何编码?

I know the location of the character, so for example, if I want to change the 3rd character in a string from A to B - how would I code that?

推荐答案

如果它总是使用相同的字符:

If it is always the same character you can use:

stringByReplacingOccurrencesOfString:withString:

如果它是同一位置的相同字符串,您可以使用:

If it is the same string in the same location you can use:

stringByReplacingOccurrencesOfString:withString:options:range:

如果只是一个特定的位置,你可以使用:

If is just a specific location you can use:

stringByReplacingCharactersInRange:withString:

此处的文档:
https://developer.apple.com/documentati on / foundation / nsstring

例如:

NSString *someText = @"Goat";
NSRange range = NSMakeRange(0,1);
NSString *newText = [someText stringByReplacingCharactersInRange:range withString:@"B"];

newText等于Boat

newText would equal "Boat"

这篇关于替换Objective-C中字符串中的一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆