This question already has answers here:
Understanding slicing
(38 answers)
Closed 1 year ago.
def add_binary(a,b):
return bin(a+b)[2:]
Why is "[2:]" used here?
convert number into it's binary and remove first two characters 0b.
Example:
Original number: 10
Binary string: 0b1010
after that it will be 1010 only.
Related
This question already has answers here:
Validation issue in email for minimum length in javascript
(2 answers)
regex for email validation (minimum characters) [closed]
(2 answers)
Email validation- characters length before # and before dot
(3 answers)
Limit length of characters in a regular expression
(6 answers)
Closed 2 years ago.
For example {limit [5-50] for here}#email.com
email#hotmail.com -> incorrect, because count of characters input smaller then 5
tryemail#hotmail.com -> correct
Can you explain the solution ?
Maybe as simple as this:
local_part, domain = email.split('#')
local_part.length < 5
Though keep in mind many people do have short addresses, so don't get too strict here.
You could also do this with a regular expression:
email.match?(/\A[^#]{5,50}#[^#]+\z/)
Where that's an extremely lax parser.
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 +
This question already has answers here:
Precision String Format Specifier In Swift
(31 answers)
Closed 8 years ago.
I’ve made a calculator using Doubles in Swift. The problem is that when I display the outcome it will display .0 at the end even if it’s a round number. I have tried the round() function but since it’s a double it still seems to always display .0 . In Objective-c i did this by typing:
[NSString stringWithFormat:#”%.0f”, RunningTotal]; //RunningTotal being the outcome
In this case there would be no decimals at all which there would if there stood #”%.3f” for example.
Does anyone know how to do this in swift? I’ve looked around on different forums but couldn’t find it anywhere... Thanks!
Your can do the same in Swift.
let runningTotal = 12.0
let string = String(format:"%.0f", runningTotal)
println(string) // Output: 12
Generally, this would round the floating point number to the next integer.
The %g format could also be used, because that does not print trailing
zeros after the decimal point, for example:
String(format:"%g", 12.0) // 12
String(format:"%g", 12.3) // 12.3
For more advanced conversions, have a look at NSNumberFormatter.
This question already has answers here:
How to format this international phone number in Rails?
(4 answers)
Closed 8 years ago.
I need to convert the phone number to international phone number example
3012944070 Output will be like this (301) 294-4070
You might want to try the built in helper http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_phone I believe it outputs in US only
Use sub:
phone = 3012944070
phone.to_s.sub(/(\d{3})(\d{3})(\d{4})/, '(\1) \2-\3')
# => "(301) 294-4070"
This question already has an answer here:
Regular expression for password complexity
(1 answer)
Closed 8 years ago.
I'm trying to have the following rules for NSString validation using regular expression:
8 characters minimum length
at least 1 digit
at least 1 uppercase
at least 1 lowercase
I'm only able to do the following to get the first rule like this:
^[a-zA-Z0-9]{8,}$
Which if i understand correctly check for minimum 8 characters length with lower/uppercase and digit
Thank you
Use a lookahead for each assertion:
(?=.*\d)(?=.*[A-Z])(?=.*[a-z])^.{8,}$