How to append string inside for loop [duplicate] - swift

This question already has answers here:
How do I convert a Swift Array to a String?
(25 answers)
Closed 6 years ago.
I have user array and I want concat values with ','
for s in userArray {
}
I want the result be A,B,C,D
How could I do this in Swift 3?

Use the array joined(separator:) function
let joined = stringArray.joined(separator: ", ")

Related

How do I get the last value in a string separated by commas? [duplicate]

This question already has answers here:
Split a String into an array in Swift?
(40 answers)
Closed 5 years ago.
I have this string
let data = 123,456,7,8,9,10
I want to extract the last value separated by a "," which in this case would be 10, and its not necessarily a two digit value.
I tried this:
var data = 123,456,7,8,9,10
data = data.last!
Use String method data.components(separatedBy:)
let data = "123,456,7,8,9,10"
let lastComponent = data.components(separatedBy: ",").last
print(lastComponent)

How can i remove all the numbers from a string in Swift 3? [duplicate]

This question already has an answer here:
How to remove numbers from a String in Swift
(1 answer)
Closed 5 years ago.
I need to remove all the numbers from a string, but the code that I have is in swift 2.3
let string = (string.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet()) as NSArray).componentsJoinedByString("")
How can I do the same in swift 3? thanks!
Try this code
let string = string.components(separatedBy: CharacterSet.decimalDigits).joined()

how can I join words from a String array to a single String, separated by a comma in Swift? [duplicate]

This question already has answers here:
How do I convert a Swift Array to a String?
(25 answers)
Closed 5 years ago.
I have an array of Strings in my Swift app. I want to display them in a label, each of them separated by ,. I tried this:
for hashtag in placeHashtags {
text = text + "\(hashtag), "
}
if (placeHashtags.count > 0){
let text1 = text.remove(at: text.index(before: text.endIndex-1))
text = text1.description
}
(the 2nd if is to remove the last comma), but then I do not see anything in my label.
How can I fix it?
You should Write this:
let array = ["Hi", "Hello", "How", "Are", "You", "Fine"]
let joined = array.joined(separator: ", ")

Faster way to create an array of numbers spanning a range in Swift [duplicate]

This question already has answers here:
Is there a way to instantly generate an array filled with a range of values in Swift?
(4 answers)
Closed 6 years ago.
Is there a shorter way to create an array of numbers spanning a range in swift?
Right now, I'm using this:
var arrayOfInts = [UInt32]()
for number in 1...100 {
arrayOfInts.append(number)
}
Is there a one-line way of doing it?
var arrayOfInts = Array(1...100)
Playground Output
Is this short enough?
let array = Array(1...100)
Try like this
var z = [Int](1...100)
print(z)
DEMO

How to append two NSMutableAttributeString in swift [duplicate]

This question already has answers here:
How to append a NSMutableString with a NSMutableString?
(4 answers)
Closed 6 years ago.
how to join two NSMutableAttributeString into single NSMutableAttributeString in swift
Simple try this:
let a = NSMutableAttributedString(string: "Hello")
let b = NSMutableAttributedString(string: "world!")
a.appendAttributedString(b)
print("final string =\(a)")