ios 如何在 Swift 中的字符串中的特定索引处添加字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27103454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a character at a particular index in string in Swift
提问by lakesh
I have a string like this in Swift:
我在 Swift 中有一个这样的字符串:
var stringts:String = "3022513240"
If I want to change it to string to something like this: "(302)-251-3240"
, I want to add the partheses at index 0, how do I do it?
如果我想将其更改为类似这样的字符串:"(302)-251-3240"
,我想在索引 0 处添加括号,我该怎么做?
In Objective-C, it is done this way:
在 Objective-C 中,它是这样完成的:
NSMutableString *stringts = "3022513240";
[stringts insertString:@"(" atIndex:0];
How to do it in Swift?
如何在 Swift 中做到这一点?
采纳答案by Dharmesh Kheni
If you are declaring it as NSMutableString
then it is possible and you can do it this way:
如果您将其声明为NSMutableString
那么它是可能的,您可以这样做:
let str: NSMutableString = "3022513240)"
str.insert("(", at: 0)
print(str)
The output is :
输出是:
(3022513240)
EDIT:
编辑:
If you want to add at starting:
如果要在开始时添加:
var str = "3022513240)"
str.insert("(", at: str.startIndex)
If you want to add character at last index:
如果要在最后一个索引处添加字符:
str.insert("(", at: str.endIndex)
And if you want to add at specific index:
如果您想在特定索引处添加:
str.insert("(", at: str.index(str.startIndex, offsetBy: 2))
回答by Dan Beaulieu
Swift 3
斯威夫特 3
Use the native Swift approach:
使用原生 Swift 方法:
var welcome = "hello"
welcome.insert("!", at: welcome.endIndex) // prints hello!
welcome.insert("!", at: welcome.startIndex) // prints !hello
welcome.insert("!", at: welcome.index(before: welcome.endIndex)) // prints hell!o
welcome.insert("!", at: welcome.index(after: welcome.startIndex)) // prints h!ello
welcome.insert("!", at: welcome.index(welcome.startIndex, offsetBy: 3)) // prints hel!lo
If you are interested in learning more about Strings and performance, take a look at @Thomas Deniau'sanswer down below.
如果您有兴趣了解有关字符串和性能的更多信息,请查看下方@Thomas Deniau 的回答。
回答by Eric
var myString = "hell"
let index = 4
let character = "o" as Character
myString.insert(
character, at:
myString.index(myString.startIndex, offsetBy: index)
)
print(myString) // "hello"
Careful: make sure that index
is bigger than or equal to the size of the string, otherwise you'll get a crash.
小心:确保index
大于或等于字符串的大小,否则会崩溃。
回答by ElvinM
var phone= "+9945555555"
var indx = phone.index(phone.startIndex,offsetBy: 4)
phone.insert("-", at: indx)
index = phone.index(phone.startIndex, offsetBy: 7)
phone.insert("-", at: indx)
var phone="+9945555555"
var indx = phone.index(phone.startIndex,offsetBy: 4)
phone.insert("-", at: indx)
index = phone.index(phone.startIndex, offsetBy: 7)
phone.insert("-", at: indx)
//+994-55-55555
//+994-55-55555
回答by Dilmurat Abduqayyum
Maybe this extension for Swift 4 will help:
也许这个 Swift 4 的扩展会有所帮助:
extension String {
mutating func insert(string:String,ind:Int) {
self.insert(contentsOf: string, at:self.index(self.startIndex, offsetBy: ind) )
}
}
回答by Maninderjit Singh
To Display 10 digit phone number into USA Number format (###) ###-####SWIFT 3
将 10 位电话号码显示为美国号码格式 (###) ###-#### SWIFT 3
func arrangeUSFormat(strPhone : String)-> String {
var strUpdated = strPhone
if strPhone.characters.count == 10 {
strUpdated.insert("(", at: strUpdated.startIndex)
strUpdated.insert(")", at: strUpdated.index(strUpdated.startIndex, offsetBy: 4))
strUpdated.insert(" ", at: strUpdated.index(strUpdated.startIndex, offsetBy: 5))
strUpdated.insert("-", at: strUpdated.index(strUpdated.startIndex, offsetBy: 9))
}
return strUpdated
}
回答by Thomas Deniau
You can't, because in Swift string indices (String.Index) is defined in terms of Unicode grapheme clusters, so that it handles all the Unicode stuff nicely. So you cannot construct a String.Index from an index directly. You can use advance(theString.startIndex, 3)
to look at the clusters making up the string and compute the index corresponding to the third cluster, but caution, this is an O(N) operation.
你不能,因为在 Swift 中字符串索引 (String.Index) 是根据 Unicode 字素簇定义的,因此它可以很好地处理所有 Unicode 内容。所以你不能直接从索引构造一个 String.Index 。您可以使用advance(theString.startIndex, 3)
查看组成字符串的集群并计算与第三个集群对应的索引,但请注意,这是一个 O(N) 操作。
In your case, it's probably easier to use a string replacement operation.
在您的情况下,使用字符串替换操作可能更容易。
Check out this blog postfor more details.
查看此博客文章了解更多详情。
回答by Pranavan Sp
You can't use in below Swift 2.0 because String
stopped being a collection
in Swift 2.0. but in Swift 3 / 4 is no longer necessary now that String
is a Collection
again. Use native approach of String
,Collection
.
您不能在 Swift 2.0 以下使用,因为在 Swift 2.0 中String
不再是 a collection
。但在 Swift 3 / 4 中不再需要,现在又String
是一个Collection
。使用原生的做法String
,Collection
。
var stringts:String = "3022513240"
let indexItem = stringts.index(stringts.endIndex, offsetBy: 0)
stringts.insert("0", at: indexItem)
print(stringts) // 30225132400
回答by Dilapidus
Swift 4.2 version of Dilmurat's answer (with code fixes)
Dilmurat 答案的 Swift 4.2 版本(带有代码修复)
extension String {
mutating func insert(string:String,ind:Int) {
self.insert(contentsOf: string, at:self.index(self.startIndex, offsetBy: ind) )
}
}
Notice if you will that the index must be against the string you are inserting into (self) and not the string you are providing.
请注意,如果您愿意,索引必须针对您插入的字符串 (self) 而不是您提供的字符串。