-3

I am trying use String(format:, ) for reading some characters from left or right, do we have something for this job?

for example reading 2 characters from left would be: "AB" like this: "%2L@"

my code:

let stringOfText = String(format: "%@", "ABCDEF")
ios coder
  • 1
  • 4
  • 31
  • 91
  • `"ABCDEF".prefix(2)`. `String(format` is not needed. – vadian Dec 23 '20 at 17:42
  • thanks, I know that I was think since we could do "%.5f" or "%05d" maybe I could do some sort of things on String as well – ios coder Dec 23 '20 at 17:47
  • No, you can't, the *decimal places modifiers* affect only numeric values. – vadian Dec 23 '20 at 17:51
  • @willy https://stackoverflow.com/a/52447981/2303865 – Leo Dabus Dec 23 '20 at 17:55
  • @ vadian: I know that as well, I do not wanted use decimal places modifiers for String, It was an example to show my idea, I wanted to know if we got any does modifiers for String as well, like this: "%2L@" "%2R@" – ios coder Dec 23 '20 at 17:56
  • @LeoDabus: that was helpful, voted! – ios coder Dec 23 '20 at 18:04
  • @willy updated the post – Leo Dabus Dec 23 '20 at 19:16
  • 1
    @LeoDabus: that is nice thanks – ios coder Dec 23 '20 at 19:40
  • @willy regarding your last question if you implement only `SignedNumeric` method, use a negative value and explicitly set the resulting type to be Uint `let add: UInt = addition(a: -3, b: -2)` Xcode will throw an error saying that `addition(a:b:)' requires that 'UInt' conform to 'SignedNumeric'` but it you implement only the Numeric version and try to do the same you get a # error `Negative integer '-3' overflows when stored into unsigned type 'UInt'`. – Leo Dabus Dec 26 '20 at 05:24

1 Answers1

0

String(format:) is usually to transform a different value type into a string.

Since you already have a string, you don't really need this method.

try: https://developer.apple.com/documentation/swift/string/2894830-prefix

Alexis
  • 71
  • 4