Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a character at a particular index in string in Swift

I have a string like this in 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?

In Objective-C, it is done this way:

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

How to do it in Swift?

like image 733
lakshmen Avatar asked Nov 24 '14 11:11

lakshmen


People also ask

How do you add a character to a string at index?

One can use the StringBuffer class method namely the insert() method to add character to String at the given position. This method inserts the string representation of given data type at given position in StringBuffer. Syntax: str.

How do I append a character to a string in Swift?

for aCharacter: Character in aString { var str: String = "" var newStr: String = str. append(aCharacter) // ERROR ... }

How do I add words to a string in Swift?

To insert a character in string at specific index in Swift, use the String method String. insert() . where str1 is the string, ch is the character and i is the index of type String. Index.

How do you get the index of a character in a string in Swift?

To get index of a substring in a string with Swift 2: let text = "abc" if let range = text. rangeOfString("b") { var index: Int = text. startIndex.


5 Answers

Swift 3

Use the native Swift approach:

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's answer down below.

like image 163
Dan Beaulieu Avatar answered Oct 12 '22 15:10

Dan Beaulieu


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

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))
like image 36
Dharmesh Kheni Avatar answered Oct 12 '22 15:10

Dharmesh Kheni


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 smaller than or equal to the size of the string, otherwise you'll get a crash.

like image 30
Eric Avatar answered Oct 12 '22 16:10

Eric


Maybe this extension for Swift 4 will help:

extension String {
  mutating func insert(string:String,ind:Int) {
    self.insert(contentsOf: string, at:self.index(self.startIndex, offsetBy: ind) )
  }
}
like image 9
Murat Uygar Avatar answered Oct 12 '22 15:10

Murat Uygar


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

like image 6
ElvinM Avatar answered Oct 12 '22 16:10

ElvinM