Swift 4: modifying numbers in String [duplicate] - swift

This question already has answers here:
Leading zeros for Int in Swift
(12 answers)
Closed 5 years ago.
In my application i create multiple files and i would like them to have consecutive numbering as part of the name. For example log_0001, log_0002
I use string interpolation, so in code it looks like
fileName = "log_\(number)"
number = number + 1
However, if i keep number as just Int, i will have log_1 instead of log_0001
I've only figured that i could check if number has 1/2/3/4 digits and add '000/00/0' manually.
Are there any String modifications allowing to put the required number of '0' to make it 4 symbols?

"I've only figured that i could check if number has 1/2/3/4 digits and add '000/00/0' manually."
In this case try the following code: Save the digit length in a variable, for example digitLength.
if (digitLength == 1) { print("log_000")
} else if (digitLength == 2) { print("log_00")
} else if (digitLength == 3) { print("log_0")
}
else { print("log_") }
print(number)
and then the variable.

Related

Return to beginning of a function in Swift [duplicate]

This question already has answers here:
Generate a Swift array of nonrepeating random numbers
(3 answers)
Closed 1 year ago.
So I've following code running in Xcode 12 with SwiftUI:
public func giveCard() -> String {
var randomNumber = Int.random(in: 0..<maxCards-1)
if saveRandoms.contains(randomNumber) {
print("Already exist - generate new number")
} else {
let actualCard = questionCard[randomNumber]
saveRandoms.add(randomNumber)
return actualCard
print("Schicke frage")
}
return "No question-card left"
}
The code creates random numbers between 0 and the variable maxCards.
When a questionCard fitting to the random number which was created was used, the random number is stored in the array saveRandoms.
To avoid showing the same question the if-statement checks if the new created random number is already in the array saveRandoms. If not, the fitting question will be shown.
If the number was already used it should generate a new number until finding a unused one.
And that's my problem. How can I return to the beginning of my function to check again and again if the random number was already used?
I think the best solution is to put the logic in a while loop and finish executing when a new number is found.

How can I return a combined Int from an [Int] array? [duplicate]

This question already has answers here:
Concatenate Swift Array of Int to create a new Int
(5 answers)
Closed 2 years ago.
I have an [Int] array like so:
[1, 2, 3]
How can I apply a function on this so it returns:
123
?
let nums = [1, 2, 3]
let combined = nums.reduce(0) { ($0*10) + $1 }
print(combined)
Caveats
Make sure the Int won't overflow if the number gets too long (+263 on a 64-bit system).
You need to also be careful all numbers in the list aren't more than 9 (a single base-10 digit), or this arithmetic will fail. Use the String concatenation technique to ensure that all base-10 numbers are correctly handled. But, again, you need to be careful that the number won't overflow if you choose to convert it back to an Int.
We can do like below...
var finalStr = ""
[1,2,3].forEach {
finalStr.append(String($0))
}
if let number = Int(finalStr) {
print(number)
}

How can I, in a if Statement ask for multiple possible numbers, Swift [duplicate]

This question already has answers here:
How to compare one value against multiple values - Swift
(8 answers)
Closed 2 years ago.
I want to ask if the generated random Number is 1 or 13 or 25. I cut ask something like:
if randomNumber == (1) || if randomNumber == (13) || if randomNumber == (25)
and that's works but its way to much code. I try to minimize my code.
I did try something like this:
if randomNumber == (1 || 13 || 25)
but this didn't worked.
You can convert them to a simple collection like an array or something and use its methods like:
if [1, 13, 25].contains(randomNumber)
More generally:
if (<#myArray#>.contains { <#condition#> }

Swift 4 How can I get the first 30 of a string [duplicate]

This question already has answers here:
How to get last 4 characters of a string? [duplicate]
(2 answers)
Closed 4 years ago.
I have just updated to Swift 4 and I am finding it difficult to show the first 30 characters of a string. I have a TableView that gets data via Json and that is populating a label. Now since Substrings can not be used anymore I am having a hard time getting this to work. I am essentially checking if the string returned has 30 or more characters and if it does then I want to show only those 30 characters else show everything since it's under 30 characters .... This is my code
if streamsModel.Posts[indexPath.row].count >= 30
{
// Show 30 characters here
cell.post.text = streamsModel.Posts[indexPath.row]
//.substringFromIndex(cell.post.text.endIndex.advancedBy(-4))
}
else
{
cell.post.text = streamsModel.Posts[indexPath.row]
}
cell.post.tag = indexPath.row
Use prefix:
cell.post.text = String(streamsModel.Posts[indexPath.row].prefix(30))

Deleting Specific Substrings in Strings [Swift] [duplicate]

This question already has answers here:
Any way to replace characters on Swift String?
(23 answers)
Closed 5 years ago.
I have a string var m = "I random don't like confusing random code." I want to delete all instances of the substring random within string m, returning string parsed with the deletions completed.
The end result would be: parsed = "I don't like confusing code."
How would I go about doing this in Swift 3.0+?
It is quite simple enough, there is one of many ways where you can replace the string "random" with empty string
let parsed = m.replacingOccurrences(of: "random", with: "")
Depend on how complex you want the replacement to be (remove/keep punctuation marks after random). If you want to remove random and optionally the space behind it:
var m = "I random don't like confusing random code."
m = m.replacingOccurrences(of: "random ?", with: "", options: [.caseInsensitive, .regularExpression])