0

I'm formatting a String from a UITextField with input type number : Example : If I have '10000', I format '10000' to have '10 000' String.

Problem : Later I need access to the Int value of this String, but when casting, I got an exception because the String is not well formatted to be casted as it contains spaces. (Example : Int("10 000") not working.)

So I want to remove spaces from the String before casting to Int, by using : myString.trimmingCharacters(in: .whitespaces) but the spaces are still here.

I'm using the following extension :

extension Formatter {
    static let withSeparator: NumberFormatter = {
        let formatter = NumberFormatter()
        formatter.groupingSeparator = " "
        formatter.numberStyle = .decimal
        return formatter
    }()
}

extension BinaryInteger {
    var formattedWithSeparator: String {
        return Formatter.withSeparator.string(for: self) ?? ""
    }
}

I also tried to retrieve the original NSNumber from my Formatter by doing :

print(Formatter.withSeparator.number(from: "10 000").intValue)

But result is nil too.

Any idea ?

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
AnthonyR
  • 3,485
  • 1
  • 18
  • 41
  • 1
    because probably the `formatter` is different from the `myFormatter`, otherwise it works smoothly, and the integer value will be `10000` for the input string `"10 000"` – btw, _trimming_ trims the leading and tailing characters only. – holex Mar 05 '18 at 14:52
  • I'm using the same from a Formatter extension – AnthonyR Mar 05 '18 at 14:53
  • then post your _real_ code you are _actually_ using as is. – holex Mar 05 '18 at 14:54
  • Thank you, your first answer about `trimming` shows me the way. I needed `replacingOccurences(...)`to remove spaces, stupid error – AnthonyR Mar 05 '18 at 15:02
  • 1
    @AnthonyR `Formatter.withSeparator.number(from: "10 000")?.intValue` prints `"Optional(10000)\n"` for me. Are you sure your string doesn't contain white spaces other than the grouping separator? – Leo Dabus Mar 05 '18 at 15:10
  • I just checked, in fact yes : At the end of my `String` there was an additional space that breaked my formatter path, this is why my number formatter was not working on the strings I was using. – AnthonyR Mar 05 '18 at 15:26
  • Please don't edit your solution into your question. Instead, post it as an answer. – Robert Columbia Mar 05 '18 at 15:31

3 Answers3

1

myString.trimmingCharacters(in: .whitespaces) will remove spaces at begin and end of string, so you need to remove all spaces between characters by below code:

let newString = myString.replacingOccurrences(of: " ", with: "")

then convert newString to Int

Moayad Al kouz
  • 1,342
  • 1
  • 9
  • 19
1

To remove leading and trailing whitespace:

let myString = "  It Is Wednesday My Dudes!   "

myString.trimmingCharacters(in: .whitespaces)

print(myString) //"It Is Wednesday My Dudes! "

To remove all whitespaces:

extension String {
    var whiteSpaceRemoved: String {
        replacingOccurrences(of: " ", with: "")
    }
}

let myString = "  It Is Wednesday My Dudes!   "

print(myString.whiteSpaceRemoved) //"ItIsWednesdayMyDudes!"
Nico S.
  • 3,056
  • 1
  • 30
  • 64
0

Solved :

There was an additional space at the end of the strings I was using. E.g : "10 000 ", so the formatter path I was using was wrong.

AnthonyR
  • 3,485
  • 1
  • 18
  • 41