0

I'm trying to create a fixed length string function in swift. I know how it's done in Java for Android but unsure how to translate it to Swift. This is what the Java function looks like:

private String getFixedLengthString(String name, int fixedLength) {
    if(name == null) {
        name = "";
    }
    if(name.length() > fixedLength) {
        name = name.substring(0, fixedLength);
    }
    return String.format("%1$-" + fixedLength + "s", name);
}

Any help is appreciated!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92
  • 3
    Sorry but you are in the wrong place. SO it is not a code conversion website. You should show what you have tried and the issues you are facing. You should take a moment and read hot to ask https://stackoverflow.com/help/how-to-ask – Leo Dabus Oct 24 '17 at 14:46
  • Read [Swift Strings documentation](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html) to find what you want – Olivier Oct 24 '17 at 14:47
  • 1
    Moreover, explaining what exactly you are trying to achieve would help a lot in receiving an answer, since in that case, someone without any Java knowledge could also help. – Dávid Pásztor Oct 24 '17 at 14:47
  • @dasblinkenlight he is trying to trim not pad – Leo Dabus Oct 24 '17 at 14:52
  • @DávidPásztor I'm trying to add a string and a and a fixed length to a function to check if the string is the same as the fixed length, but if the string is nil, return empty space, " ". – SwiftyJD Oct 24 '17 at 14:52
  • @SwiftyJD Show what you have tried first. Nobody should do your homework for you – Leo Dabus Oct 24 '17 at 14:55
  • @LeoDabus He tries to do both - his `substring` line trims, while his `return` line pads. – Sergey Kalinichenko Oct 24 '17 at 14:56
  • @SwiftyJD I haven't written Java code for a long time, but if I get your code write, that's not what the Java function does. You want to return a String looking like `"\(input/substring)-\(fixedLength)"`, where input/substring consists of the first `fixedLength` characters of `name`? – Dávid Pásztor Oct 24 '17 at 14:58
  • @DávidPásztor yes, something like that – SwiftyJD Oct 24 '17 at 15:03

2 Answers2

0
    func getFixedLengthString(_ name: String?, _ fixedLength: Int) -> String{
    var myName = name
    if myName == nil{
        myName = ""
    }

    if myName!.characters.count > fixedLength{
        myName = myName?.substring(to: fixedLength)
    }
    return "\(myName!)-\(fixedLength)s"
    }

try this

Enea Dume
  • 3,014
  • 3
  • 21
  • 36
  • 1
    substring it is deprecated Swift4 – Leo Dabus Oct 24 '17 at 14:52
  • 2
    I did'nt say it didn't work. Anyway you should unwrap your optional instead of checking against nil and forcing unwrap it later – Leo Dabus Oct 24 '17 at 14:54
  • you can substituate substring with myName = myName[..fixedLength] – Enea Dume Oct 24 '17 at 14:56
  • i force unwrapp becouse if it is nil i give an empty string "" – Enea Dume Oct 24 '17 at 14:57
  • Just use nil coalescing operator `??` and provide a default value to your string – Leo Dabus Oct 24 '17 at 14:58
  • yes you are wright, but he is newbie, and he wants it as in android, so i was making something equivalent for him – Enea Dume Oct 24 '17 at 15:00
  • 1
    @eneadume this is giving a error on the .substring - "Cannot convert value of type 'Int' to expected argument type 'String.Index'" – SwiftyJD Oct 24 '17 at 15:08
  • @eneadume fixedLength should be a String.Index not an Int. Btw if you would like to subscript String with an Int you can take a look at this answer https://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language/38215613?s=1|43.0600#38215613. It also supports partial range operator – Leo Dabus Oct 24 '17 at 15:17
0

You can use the prefix function to get the first fixedLength characters of a String. The output String format can easily be achieved using String interpolation.

func getFixedLengthString(name:String?,fixedLength:Int)->String{
    if let name = name {
        if name.count > fixedLength {
            return "\(name.prefix(fixedLength))-\(fixedLength)s"
        } else {
            return "\(name)-\(fixedLength)s"
        }
    } else {
        return "-\(fixedLength)s"
    }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116