[英]Check if the last char is dot (.)
I need to check if the last character of an input ( type="number"
) value is a dot!我需要检查输入(
type="number"
)值的最后一个字符是否是点!
Tried these, but it doesn't work:尝试了这些,但它不起作用:
let amount = $("#amount").val();
let last_char = amount.slice(-1);
if(last_char == ".")
{
return;
}
Also tried amount[amount.length-1]
and charAt()
还尝试
amount[amount.length-1]
和charAt()
PS Input type is type="number"
PS 输入类型为
type="number"
None of the answers work: https://jsfiddle.net/dne37hao/没有一个答案有效: https://jsfiddle.net/dne37hao/
const string = 'Is this a question.'; console.log(string.endsWith('.'));
You can achieve this using different ways but with different performance,您可以使用不同的方式但具有不同的性能来实现这一点,
1. Using bracket notation: 1.使用括号表示法:
var str = "Test";
var lastLetter = str[str.length - 1];
But it's not recommended to use brackets.但不建议使用括号。
2. charAt[index]: 2. charAt[索引]:
var lastLetter = str.charAt(str.length - 1)
This is readable and fastest among others.这是可读且最快的。 It is most recommended way.
这是最推荐的方式。
3. substring: 3. substring:
str.substring(str.length - 1); str.substring(str.length - 1);
4. slice: 4.切片:
if(str.slice(-1)=== '.'){
return true
}
It's slightly faster than substring.The most efficient one比substring稍微快一点。效率最高的一个
With ES6:使用 ES6:
You can use str.endsWith("t");
您可以使用
str.endsWith("t");
You can try this:你可以试试这个:
let amount = $("#amount").val();
if(amount[amount.length-1] === "."){
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.