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)
Related
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
This question already has answers here:
Get nth character of a string in Swift
(47 answers)
Closed 3 years ago.
I am trying to extract a partial String from a given input String in Swift5. Like the letters 2 to 5 of a String.
I was sure, something as simple as inputString[2...5]would be working, but I only got it working like this:
String(input[(input.index(input.startIndex, offsetBy: 2))..<(input.index(input.endIndex, offsetBy: -3))])
... which is still using relative positions (endIndex-3 instead of position #5)
Now I am wondering where exactly I messed up.
How do people usually extract "cde" from "abcdefgh" by absolute positions??
I wrote the following extension for shorthand substrings without having to deal with indexes and casting in my main code:
extension String {
func substring(from: Int, to: Int) -> String {
let start = index(startIndex, offsetBy: from)
let end = index(start, offsetBy: to - from + 1)
return String(self[start ..< end])
}
}
let testString = "HelloWorld!"
print(testString.substring(from: 0, to: 4)) // 0 to 4 inclusive
Outputs Hello.
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 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)
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: ", ")