How do you confirm a string only contains numbers in Swift? [duplicate] - ios

This question already has answers here:
Finding out whether a string is numeric or not
(18 answers)
Closed 7 years ago.
How can I check, if searchView contains just numbers?
I found this:
if newText.isMatchedByRegex("^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$") { ... }
but it checks if text contains any number. How can I do, that if all text contains just numbers in Swift?

Here is the solution you can get all digits from String.
Swift 3.0 :
let testString = "asdfsdsds12345gdssdsasdf"
let phone = testString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
print(phone)

you can use "^[0-9]+$" instade "^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"
This will accept one or more digits, if you want to accept only one digit then remove +

Related

Localize file with property in swift [duplicate]

This question already has answers here:
Precision String Format Specifier In Swift
(31 answers)
Closed 2 years ago.
I need to transfer the double variable to the localization file, if I write %d I can only pass int, but I need to pass the double
You can pass the double with format string %f. To restrict the number of digits after the decimal point you can format is like this %0.2f. This will allow only 2 digits after the decimal point.
Sample Code:
let num = 10.5
let outputStr = String(format:"I am printing %0.1f", arguments:[num])
print(outputStr)

Capitalize First Letter of each word in a string (Swift) [duplicate]

This question already has answers here:
How to capitalize each word in a string using Swift iOS
(8 answers)
Closed 5 years ago.
I want output like this:
var String1 = "stack over flow"
var desiredOutPut = "Stack Over Flow"
Also, I want this output in TextField event named "textFieldDidChange" so it must be efficient as well.
var desiredOutPut = String1.capitalized

Swift Character + Int operation [duplicate]

This question already has answers here:
How to convert an Int to a Character in Swift
(9 answers)
Closed 6 years ago.
I'm rewriting a library from java which uses something like
public int myFunc(char c){
return c+200
}
because in java 'a' + 1 will be 'b'
in Swift I cannot simply add an Int to a Character. Google search doesn't know anything about that.
Swift is a Strong Type Language. So the typecast is strict.
If you get ASCII value, you can like this:
let asciiValues = string.utf8.map{ Int($0) }
I hope that helps.

Swift Iteration Character Length [duplicate]

This question already has answers here:
Generate random alphanumeric string in Swift
(24 answers)
Closed 6 years ago.
I am in the process of making one of my first apps, the aim of it is as a password generator and telling people on a scale of 1-1000 how hard it is to guess, and how hard it is to remember based on how the letters are formatted and what it looks like and how the brain remembers patterns. So far i have all the characters I want to use in an array, and I then have a for in loop that iterates through the characters, but I can't figure out how to specify the length of the password to generate, as currently it just prints each character. So, I am asking how can I make an 8 character long password generator as simply as possible, what i have so far is:
import Foundation
let chars = ["a","b","c","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u" ,"v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"]
Thank you!
var generate: String
for generate in chars {
print(generate)
}
As simply as possible use a loop and and a random number to get the character at given index:
let length = 8
var pass = ""
for _ in 0..<length {
let random = arc4random_uniform(UInt32(chars.count))
pass += chars[Int(random)]
}
print(pass)

How to add percent sign in string format and using swift language? [duplicate]

This question already has answers here:
How to add percent sign to NSString
(7 answers)
Closed 6 years ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I want to get string like "99.99%" from Double,and I used format to get this:
let rate = 99.99999
let str = String(format: "%.2f%", rate)
// output 99.99
And \% is not allowed. So how to add percent sign in string format, please help me!
write % twice:
rate = 99.99
let str = String(format: "%.2f%%", rate)
The % symbol has a special use in a printf statement. How would you
place this character as part of the output on the screen?
You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.
Hope it help you :)

Resources