2

I have a String "000". I want to change this to "0.00".

I took a look at the insert function.

var str = "000"
str.insert(".", at: str.endIndex)

How do I get the index of 2 before the end index?

I tried:

str.insert(".", at: str.endIndex - 1)

but this didn't work at all.

3
  • 6
    str.insert(".", at: str.index(str.endIndex, offsetBy: -2)). Don't forget to make sure your string characters count > 2 Commented Jul 8, 2017 at 15:14
  • Thank you so much! Commented Jul 8, 2017 at 15:16
  • stackoverflow.com/a/32466063/1187415 Commented Jul 8, 2017 at 15:17

2 Answers 2

1

You could also use Strings character property. Its basically an array made up of all the characters (duh) in the String.

So you would:

var str = "000"

let index = str.characters.index(str.characters.startIndex, offsetBy: 1)  //here you define a place (index) to insert at
str.characters.insert(".", at: index)  //and here you insert

Unfortunately you have to create an index first, as .insert does not allow you to specify the position using an Int.

Sign up to request clarification or add additional context in comments.

2 Comments

It is a collection, not an array (otherwise you could index it with an Int). Note that your code is equivalent to let index = str.index(str.startIndex, offsetBy: 1) ; str.insert(".", at: index) since String forwards those calls to its characters view.
That's pretty neat! But there isn't a way around creating an index first, right? Seems tedious.
1

Since Swift 2, String does no longer conform to SequenceType. In other words, you can not iterate through a string with a for...in loop.

The simple and easy way is to convert String to Array to get the benefit of the index just like that:

let input = Array(str)

I remember when I tried to index into String without using any conversion. I was really frustrated that I couldn’t come up with or reach a desired result, and was about to give up. But I ended up creating my own workaround solution, and here is the full code of the extension:

extension String {
    subscript (_ index: Int) -> String {
    
        get {
             String(self[self.index(startIndex, offsetBy: index)])
        }
    
        set {
            if index >= count {
                insert(Character(newValue), at: self.index(self.startIndex, offsetBy: count))
            } else {
                insert(Character(newValue), at: self.index(self.startIndex, offsetBy: index))
            }
        }
    }
}

Now that you can read and write a single character from string using its index just like you originally wanted to:

var str = "car"
car[3] = "d"
print(str)

It’s simple and useful way to use it and get through Swift’s String access model. Now that you’ll feel it’s smooth sailing next time when you can loop through the string just as it is, not casting it into Array.

Try it out, and see if it can help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.