How can I move automatically to next input after writing 4 digit in a date input year value - forms

right now if i type in date input year value the year value is still taking input after typing 4 digit i want to move focus automatically to next input after year value is typed in 4 digit
and not accept values more then 4 digit
<Form.Control type='date' max='9999-12-31' max-length='8' // pattern='[0-3][0-9].[01][0-9].[0-9]{4}' size='sm' value={initialValues.birthDate} onChange={(e: any) => {setInitialValues({ ...initialValues, birthDate: e.target.value })}}/>
input field year lenth is max 4 but still when user is pressing 5th value then this will delete the value of index 0 ind repalce with index value 1

// the pseudo code be like
dateInput.addEventListener('change', (ev) => {
let l = ev.target.value.length;
if(l >= 4) {
getNextElement().focus();
}
})

Related

Flutter int value if first digit is not zero I want to get first value

Flutter int value if first digit is not zero I want to get first value
for example
int x=123
result =1;
another example
int y=234;
result=2;
if the first value is zero I want to get the second value
for example
int a=023;
result=2;
another example
int b=098;
result=9;
how can i do this using dart?
There surely is a more mathematical way, but one way to do this is to convert the int to a String, get the first character and parse it back to int :
var numbers = [
1234,
567,
89,
];
for(var number in numbers) {
var firstNumber = int.parse(number.toString()[0]);
print(firstNumber);
}
Output :
1
5
8
This will give you the first non-zero digit. Assuming you get your number as a String. Otherwise it is pointless because the int value = 0123 is the same as int value = 123.
String yourNumber = "0123";
final firstNonZero = int.tryParse(yourNumber.split('').firstWhere((element) => element != '0', orElse: () => ''));
print(firstNonZero);

indexOf second argument in Scala

I want to understand what does second argument in indexOf in Scala mean for Strings?
object Playground extends App {
val g: String = "Check out the big brains on Brad!"
println(g.indexOf("o",7));
}
The above program returns: 25 which is something I am not able to understand why?
It is actually the index of last o but how is it related to 7? Is it like the second argument n returns the index of the occurence of nth character and if n exceeds with the number of occurences then it returns the index of last present element?
But if that's the case then this doesn't make sense:
object Playground extends App {
val g: String = "Check out the big brains on Brad!"
(1 to 7).foreach(i => println(s"$i th Occurence = ${g.indexOf("o",i)} "))
}
which outputs:
1 th Occurence = 6
2 th Occurence = 6
3 th Occurence = 6
4 th Occurence = 6
5 th Occurence = 6
6 th Occurence = 6
7 th Occurence = 25
Source: https://www.scala-exercises.org/std_lib/infix_prefix_and_postfix_operators
According to Scala String documentation, the second parameter is the index to start searching from:
def indexOf(elem: Char, from: Int): Int
Finds index of first occurrence of some value in this string after or at some start index.
elem : the element value to search for.
from : the start index
returns : the index >= from of the first element of this string that is equal (as determined by ==) to elem, or -1, if none exists.
Thus, in your case, when you specify 7, it means that you will look for the index of the first character "o" which is located at index 7 or after . And indeed, in your String you have two "o", one at index 6, one at index 25.
indexOf(int ch, int fromIndex) looks for the character in the string from the specified index (fromIndex). This means it starts looking at 7th position.
Going forward you need to learn to read the official docs: indexOf:
def indexOf(elem: A, from: Int): Int
[use case] Finds index of first occurrence of some value in this general sequence after or at some start index.
Note: may not terminate for infinite-sized collections.
elem
the element value to search for.
from
the start index
returns
the index >= from of the first element of this general sequence that is equal (as determined by ==) to elem, or -1, if none exists.
I personally like to use Intellij to jump into the source code with CMD + B.
No matter how you take it: in your development flow you'll frequently access the manual\docs of the lib or lang you're using.

Number validation and formatting

I want to format, in real time, the number entered into a UITextField. Depending on the field, the number may be an integer or a double, may be positive or negative.
Integers are easy (see below).
Doubles should be displayed exactly as the user enters with three possible exceptions:
If the user begins with a decimal separator, or a negative sign followed by a decimal separator, insert a leading zero:
"." becomes "0."
"-." becomes "-0."
Remove any "excess" leading zeros if the user deletes a decimal point:
If the number is "0.00023" and the decimal point is deleted, the number should become "23".
Do not allow a leading zero if the next character is not a decimal separator:
"03" becomes "3".
Long story short, one and only one leading zero, no trailing zeros.
It seemed like the easiest idea was to convert the (already validated) string to a number then use format specifiers. I've scoured:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
and
http://www.cplusplus.com/reference/cstdio/printf/
and others but can't figure out how to format a double so that it does not add a decimal when there are no digits after it, or any trailing zeros. For example:
x = 23.0
print (String(format: "%f", x))
//output is 23.000000
//I want 23
x = 23.45
print (String(format: "%f", x))
//output is 23.450000
//I want 23.45
On How to create a string with format?, I found this gem:
var str = "\(INT_VALUE) , \(FLOAT_VALUE) , \(DOUBLE_VALUE), \(STRING_VALUE)"
print(str)
It works perfectly for integers (why I said integers are easy above), but for doubles it appends a ".0" onto the first character the user enters. (It does work perfectly in Playground, but not my program (why???).
Will I have to resort to counting the number of digits before and after the decimal separator and inserting them into a format specifier? (And if so, how do I count those? I know how to create the format specifier.) Or is there a really simple way or a quick fix to use that one-liner above?
Thanks!
Turned out to be simple without using NumberFormatter (which I'm not so sure would really have accomplished what I want without a LOT more work).
let decimalSeparator = NSLocale.current.decimalSeparator! as String
var tempStr: String = textField.text
var i: Int = tempStr.count
//remove leading zeros for positive numbers (integer or real)
if i > 1 {
while (tempStr[0] == "0" && tempStr[1] != decimalSeparator[0] ) {
tempStr.remove(at: tempStr.startIndex)
i = i - 1
if i < 2 {
break
}
}
}
//remove leading zeros for negative numbers (integer or real)
if i > 2 {
while (tempStr[0] == "-" && tempStr[1] == "0") && tempStr[2] != decimalSeparator[0] {
tempStr.remove(at: tempStr.index(tempStr.startIndex, offsetBy: 1))
i = i - 1
if i < 3 {
break
}
}
}
Using the following extension to subscript the string:
extension String {
subscript (i: Int) -> Character {
return self[index(startIndex, offsetBy: i)]
}
}

Applescript: return specific index positions from a date string

I have already used the text delimiters and item numbers to extract a date from a file name, so I'm clear about how to use these. Unfortunately the date on these particular files are formatted as "yyyyMMdd" and I need to covert the date into format "yyyy-MM-dd". I have been trying to use the offset function to get particular index positions, and I have found several examples of how you would return the offset of particular digits in the string, example:
set theposition to offset of 10 in theString -- this works
(which could return 5 or 7) but I have not found examples of how to call the digits at a specific index:
set _day to offset 7 of file_date_raw -- error
"Finder got an error: Some parameter is missing for offset." number -1701
How would you do this, or is there a totally better way I'm unaware of?
To "call the digits at a specific index", you use:
text 1 thru 4 of myString
If you know that each string has 8 characters in the yyyymmdd format, then you don't need to use 'offset' or any parsing, just add in the -'s, using text x thru y to dissect the string.
set d to "20011018"
set newString to (text 1 thru 4 of d) & "-" & (text 5 thru 6 of d) & "-" & (text 7 thru 8 of d)

Removing Digits from a Number

Does anybody know if there is a way of removing (trimming) the last two digits or first two digits from a number. I have the number 4131 and I want to separate that into 41 and 31, but after searching the Internet, I've only managed to find how to remove characters from a string, not a number. I tried converting my number to a string, then removing characters, and then converting it back to a number, but I keep receiving errors.
I believe I will be able to receive the first two digit by dividing the number by 100 and then rounding the number down, but I don't have an idea of how to get the last two digits?
Does anybody know the function to use to achieve what I'm trying to do, or can anybody point me in the right direction.
Try this:
var num = 1234
var first = num/100
var last = num%100
The playground's output is what you need.
You can use below methods to find the last two digits
func getLatTwoDigits(number : Int) -> Int {
return number%100; //4131%100 = 31
}
func getFirstTwoDigits(number : Int) -> Int {
return number/100; //4131/100 = 41
}
To find the first two digit you might need to change the logic on the basis of face value of number. Below method is generalise method to find each digit of a number.
func printDigits(number : Int) {
var num = number
while num > 0 {
var digit = num % 10 //get the last digit
println("\(digit)")
num = num / 10 //remove the last digit
}
}