如何在JavaScript中进行不区分大小写的比较?
我正在制作一个简单的切换功能:如何在JavaScript中进行不区分大小写的比较?,javascript,Javascript,我正在制作一个简单的切换功能: var game = prompt("What game do you want to play?"); switch(game) { case 'League of Legends': console.log("You just played that!"); //more case statements snipped... // break; } 如果用户将league of legends(传奇联盟),而不是le
var game = prompt("What game do you want to play?");
switch(game) {
case 'League of Legends':
console.log("You just played that!");
//more case statements snipped... //
break;
}
如果用户将league of legends(传奇联盟),而不是league of legends(传奇联盟),那么它会捕捉到这一点,还是会重置为我所有案例底部的默认答案?此外,我将如何更改它,使两个答案(大写和非大写)都起作用?打开游戏的小写版本:
switch (game.toLowerCase()) {
case 'league of legends':
console.log("You just played that!");
break;
}
打开游戏的小写版本:
switch (game.toLowerCase()) {
case 'league of legends':
console.log("You just played that!");
break;
}
您可以使用String.toLowerCase或String.ToLocalLowercase
您可以使用String.toLowerCase或String.ToLocalLowercase
字符串比较区分大小写,因此,如果代码需要区分大小写,则应使用yourString.toUpperCase将所有内容大写,或使用yourString.toLowerCase将所有内容小写。字符串比较区分大小写,因此,如果您的代码需要不区分大小写,您应该使用yourString.toUpperCase将所有内容大写,或者将yourString.toLowerCase与小写一起使用 在javascript中,字符“L”不等于字符“L”。因此“传奇联盟”无法与“传奇联盟”相匹敌。通过将用户输入转换为所有小写,然后将其与switch语句中的小写字符串相匹配,可以保证程序不会根据大小写进行区分 你的代码使用toLowerCase 在javascript中,字符“L”不等于字符“L”。因此“传奇联盟”无法与“传奇联盟”相匹敌。通过将用户输入转换为所有小写,然后将其与switch语句中的小写字符串相匹配,可以保证程序不会根据大小写进行区分 你的代码使用toLowerCase
字符串“league of legends”和“league of legends”是不同的,因此在所有案例的底部都会使用默认值 要使其工作,您可以使用
switch(game.toUpperCase()) {
case 'LEAGUE OF LEGENDS':
console.log("You just played that!");
//more case statements snipped... //
break;
}
字符串“league of legends”和“league of legends”是不同的,因此在所有案例的底部都会使用默认值 要使其工作,您可以使用
switch(game.toUpperCase()) {
case 'LEAGUE OF LEGENDS':
console.log("You just played that!");
//more case statements snipped... //
break;
}
听起来您正在寻找JavaScript中不区分大小写的比较。你查过了吗?可能吧。第一次剪切时,将所有内容都小写?使用game.toLowerCase将所有内容都小写。听起来,您正在寻找JavaScript中不区分大小写的比较。你查过了吗?也许吧。把所有东西都小写作为第一个剪切?用game把它全部小写。toLowerCaseThanks谢谢你的帮助。这里几乎每个人都知道我在说什么。我研究了一下,但可能措辞不当,因为我找不到我想要的东西。但这正是它。谢谢你。谢谢你的帮助。这里几乎每个人都知道我在说什么。我研究了一下,但可能措辞不当,因为我找不到我想要的东西。但这正是它,谢谢你。