![](/img/trans.png)
[英]How can I call one JavaScript function in another JavaScript function?
[英]How can i difference between one and string and another one in JavaScript?
我在警報提示中鍵入了很多單詞,例如:“USB”,但我的錯誤是當我嘗試與相同的字符串進行比較但小寫時,如果我鍵入“usb”,我怎么能與“USB”進行比較發出一個警告,說“字符串是相同的”,我做同樣的但是當第一個字母是大寫的例如“你好”和“你好”時,但我怎么能比較我的字符串是否完全是大寫的?
我試着這樣做
var res = document.getElementById("answer").value;
var resp = res.charAt(0).toUpperCase()+ res.slice(1);
if(respuesta == textoALT.charAt(0).toUpperCase()+ textoALT.slice(1))
alert("bla bla");
if(respuesta.toLowerCase() == textoALT.toLowerCase()) {
// do something
給定兩個字符串a
和b
:
if (a === b) {
// the strings are the same text in the same case
}
if (a.toLowerCase() === b.toLowerCase()) {
// the strings are the same text but in a different case
}
你可以用兩種方式做:
a)區分大小寫
if (a === b) {
// the strings are the same text in the same case
}
記得使用===
運算符,因為它意味着a和b是相同的類型和值。
運算符==
將僅比較值。
b)不區分大小寫 - 檢查是否給出了兩個值
if (
(a && b) && // optionally to ensure both values are defined:)
(a.toLowerCase() === b.toLowerCase())
) {
// the strings are the same text but in a different case
}
這里不需要使用charAt。 你可以使用toUpperCase()
var str = "USB";
var str1 = "usb";
alert((str==str1.toUpperCase()));
用於一般目的。
alert((str.toUpperCase()==str1.toUpperCase()));
那將是真的。
if (a.toLowerCase() === b.toLowerCase()) {
// strings match regardless of case
}
注意,您幾乎應該總是使用“===”而不是“==”。 “===”測試某些值和數據類型(數字,字符串,布爾值,對象等)是否與另一個匹配,而“==”僅測試值是否匹配(執行類型轉換后)。 例如:
if ("42" == 42) { // true
if ("42" === 42) { // false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.