如何在Swift中的字符串中的特定索引处添加字符

问题描述:

我在Swift中有这样的字符串:

I have a string like this in Swift:

var stringts:String = "3022513240"

如果我想将其更改为字符串,则为:(302)-251-3240 ,我想在索引0处添加分数,我该怎么做?

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?

在Objective-C中,这样做是这样的:

In Objective-C, it is done this way:

 NSMutableString *stringts = "3022513240";
 [stringts insertString:@"(" atIndex:0];

怎么做在Swift?

如果你宣称它是 NSMutableString 那么它是可能的,你可以这样做:

If you are declaring it as NSMutableString then it is possible and you can do it this way:

var str : NSMutableString = "3022513240)"
    str.insertString("(", atIndex: 0)
    print(str)

输出为:

(3022513240)