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
Related
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)
This question already has answers here:
Swift: How to get substring from start to last index of character
(23 answers)
Closed 4 years ago.
In swift 4 you can use the following method to extract a substring:
let str = "abcdefghci"
let index = str.index(of: "c") ?? str.endIndex
print(str[...index])
This will print abc
But how can I find the index of the last c, to extract a substring from that location ?
UPDATE
#vadian please see the attached image:
Use range(of which can search backwards and use lowerBound as end index.
let str = "abcdefghci"
if let range = str.range(of: "c", options: .backwards) {
print(str[...range.lowerBound])
}
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()
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: ", ")
This question already has answers here:
Swift How to get integer from string and convert it into integer
(10 answers)
Closed 6 years ago.
I receive from a server API a String like "14px". How to transform this String to Int with value 14 ignoring substring "px"?
If string always come with just px at last you can subscript it and ignore the last 2 characters.
let str = "14px"
var numStr = str.substring(to: str.index(name.endIndex, offsetBy: -2))
print(numStr) // "14"
//Now you can convert "14" to Int
print(Int(numStr))
Or
print(Int(String(str.characters.dropLast(2))))