Remove all line breaks at the beginning of a string in Swift

If it is acceptable that newline (and other whitespace) characters are removed from both ends of the string then you can use let string = “\n\nBLA\nblub” let trimmed = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) // In Swift 1.2 (Xcode 6.3): let trimmed = (string as NSString).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) To remove leading newline/whitespace characters only you can (for example) use a regular … Read more

Efficiently repeat a character/string n times in Scala

For strings you can just write “abc” * 3, which works via StringOps and uses a StringBuffer behind the scenes. For characters I think your solution is pretty reasonable, although char.toString * n is arguably clearer. Do you have any reason to suspect the List.fill version isn’t efficient enough for your needs? You could write … Read more

Groovy String to Date

The first argument to parse() is the expected format. You have to change that to Date.parse(“E MMM dd H:m:s z yyyy”, testDate) for it to work. (Note you don’t need to create a new Date object, it’s a static method) If you don’t know in advance what format, you’ll have to find a special parsing … Read more