How to append two NSMutableAttributeString in swift [duplicate] - swift

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)")

Related

Swift 5: 'substring(to:)' is deprecated [duplicate]

This question already has answers here:
How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator
(21 answers)
Closed 3 years ago.
I`m newbie in Swift and I have simple code, that cut string from the end to special characters "$2F" and returned cutted off string:
let index2 = favUrlString.range(of: "%2F", options: .backwards)?.lowerBound
favUrlString = index2.map(favUrlString.substring(to:))!
How I have to update this code to Swift 5?
Use String Slicing instead of Substring. like below
let yourString = "dummy string"
yourString[5..<yourString.count] // string

Replace characters in Swift String? [duplicate]

This question already has answers here:
Any way to replace characters on Swift String?
(23 answers)
Closed 4 years ago.
I have a string that contains a comma. However, I would like to replace the comma by a point. This must be possible unfortunately I can not get it done. I'm not just getting back the .5. The comma is away only everything that stonged for it unfortunately too.
let cijfers = "38,5"
let start = cijfers.startIndex;
let end = cijfers.index(cijfers.startIndex, offsetBy: 3);
let result = cijfers.replacingCharacters(in: start..<end, with: ".")
print(result)
You use cijfers.replacingOccurrences not on the correct way, for your purpose.
Try this:
let str = "38,5"
let replaced = str.replacingOccurrences(of: ",", with: ".")
print(replaced)

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 to append string inside for loop [duplicate]

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: ", ")