Getting partial String in Swift5 [duplicate] - swift

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.

Related

Building a string by repeating a single character [duplicate]

This question already has answers here:
Repeating String in Swift
(8 answers)
Closed 5 months ago.
To size a view, I needed to build a string by repeating a single character. I came up with the following:
var str = ""
for _ in 0 ..< length {
str.append("W")
}
I also came up with a functional alternative:
let str = (0 ..< digits).reduce("") { (result, _) -> String in
result + "0" // assuming 0 is the most wide number
}
Both feel a bit verbose. Is there a shorter way or a built-in function in Swift?
Use init(repeating:count:)
var str = String(repeating: "w", count: length)

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)

find last index of a string in swift [duplicate]

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])
}

How to parse a String like "14px" to get Int value 14 in Swift [duplicate]

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

How do I convert Float to Int When Necessary? [duplicate]

This question already has answers here:
Swift - How to remove a decimal from a float if the decimal is equal to 0?
(15 answers)
Closed 6 years ago.
So, I'm pretty new to swift and Xcode and might be missing something obvious, but I've done a bit of research, and can't find my answer.
My code is:
for number in currentList {
listPreview.text = "\(listPreview.text!) \(String(number))"
}
The problem is, the Numbers in currentList are Floats. But if my user were to input a number that isn't a float, it will display as:
"UsersNumber".0
I want it to display as just:
"UsersNumber"
However, if the number the user gave me was a float, say... 1.2, I would still want it to display as 1.2 .
is there some kind of extension that can do this?
Like an if-statement saying
if number.isUselessFloat {
code
}
Thanks in advance,
-Another Nooby user
let number1 = 1.0
let number2 = 1.2
let str = String(format: number1 == floor(number1) ? "%.0f":"%.1f", number1)
print(str)
//prints 1
let str2 = String(format: number2 == floor(number2) ? "%.0f":"%.1f", number2)
print(str2)
//prints 1.2