How to concatenate string with brackets in Swift 2
in simple i know how to concatenate the string but if i want to brackets inside the string
let a = "Hello"
let b = "World"
let first = "(a) Per Level (b)" // i want to show this output ?
output would be like this : (Hello) Per Level (World)
Another format you can use with the latest Swift2:
let first = "(\(a)) Per Level (\(b))"
Try this:
let first = String(format: "(%#) Per Level (%#)", a, b)
Related
I have a Double like this:
339.09965653839
How can I get the first number after the dot?
0 // desired result
I tried to look into similar questions but unfortunately I found only ways to separate a double in two parts also these two Double. I would like to have an Int and then be able to print it as a string.
To get the digit as an Int, you can do a little math:
let someNum = 339.09965653839
let digit = Int((someNum - Double(Int(someNum))) * 10)
And of course it is trivial to display digit in/as a string as needed.
Or, alternatively:
let number = 123.456
// 1234 % 123 = 4
let digit = Int(number * 10) % Int(number)
Convert double to string and fetch the first character after dot.
let value = 339.09965653839
let valueInString = "\(value)".split(separator: ".")
print(valueInString[1].first ?? "")
I have an array like:
["2018-03-21 11:09:25","2018-03-22 11:09:25","2018-03-23 11:09:25","2018-03-24 11:09:25"]
I need to display only dates [2018-03-21] in this array. How to split this array?
Considering you have and want Strings, here is a way you could use with Swift 4:
var myArray = [String]()
for date in dateArray {
myArray.append(date.split(" ")[0])
}
You have to split your task in subtasks to make it easy for you.
How to transform your data?
Given an your input 2018-03-21 11:09:25 and your ouput 2018-03-21, there are several ways.
I see 3 regular ways here (there are more of course):
Splitting with the space character as a delimiter and take the first part
Using a DateFormatter
Using substring to the 10th character
As 2. seems overkill and 3. would need to work with ranges (I'm lazy), let's take 1. as an example:
let ouput = input.split(" ")[0]
How to apply to the whole array?
You have many options, again, but the simpler is map.
Given your initial array is called array.
Solution
let result = array.map { $0.split(" ")[0] }
I have two variables, string0 and string1. I want to randomly set a label to one of those strings. I tried generating the variable name using a random number like this:
let string0a = "\(Name!) sees something that offends \(Gender!)."
let string1a = "\(Name!) saw Star Wars earlier."
let number = arc4random_uniform(1)
text1.text = string\(number!)a
What is the best way to randomly set the label text to one of the two strings?
You can't generate variable names at runtime in Swift because they are used at compile time. This is what arrays were invented for:
let strings = [string0a, string1a]
let number = Int(arc4random_uniform(2))
text1.text = strings[number]
or more concisely:
text1.text = [string0a, string1a][Int(arc4random_uniform(2))]
I have a word that is being displayed into a label. Could I program it, where it will only show the last 2 characters of the word, or the the first 3 only? How can I do this?
Swift's string APIs can be a little confusing. You get access to the characters of a string via its characters property, on which you can then use prefix() or suffix() to get the substring you want. That subset of characters needs to be converted back to a String:
let str = "Hello, world!"
// first three characters:
let prefixSubstring = String(str.characters.prefix(3))
// last two characters:
let suffixSubstring = String(str.characters.suffix(2))
I agree it is definitely confusing working with String indexing in Swift and they have changed a little bit from Swift 1 to 2 making googling a bit of a challenge but it can actually be quite simple once you get a hang of the methods. You basically need to make it into a two-step process:
1) Find the index you need
2) Advance from there
For example:
let sampleString = "HelloWorld"
let lastThreeindex = sampleString.endIndex.advancedBy(-3)
sampleString.substringFromIndex(lastThreeindex) //prints rld
let secondIndex = sampleString.startIndex.advancedBy(2)
sampleString.substringToIndex(secondIndex) //prints He
I am working on an application that needs to do some displaying and calculating of Double types in Swift. The struct that I created takes a Double as a parameter, and since it also displays it, I would like to keep the trailing zero at the end of the value. However, I cannot seem to keep the trailing zero from being truncated.
Here is what I have been trying in a Playground:
let numberString = "-3.60"
// "3.60"
let positiveString = numberString.stringByReplacingOccurrencesOfString("-", withString: "", options: nil, range: nil)
// 3.6
let positiveDouble = (positiveString as NSString).doubleValue
// "3.60"
let positiveDoubleString = NSString(format: "%0.02f", positiveDouble)
// 3.6
let positiveDoubleWithZeros = positiveDoubleString.doubleValue
I keep getting 3.6 as the result, no matter what I try. What is the obvious part of the conversion that I am missing?
You have to use NSNumberFormatter:
let formatter = NSNumberFormatter()
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 3
println(formatter.stringFromNumber(1.0000))
println(formatter.stringFromNumber(1.2345))
This example will print 1.00 for the first case and 1.234 for the second, the number of minimum and maximum decimal places can be adjust as you need.
I Hope that helps you!