简体   繁体   English

如何在JavaScript中将用户输入转换为大写?

[英]How do i turn user input into upper case in JavaScript?

Hey this should be an easy question I was wondering how and if i could possibly make this if statement check for "hello" regardless if it's written hello, HELLO, heLLO ect. 嘿,这应该是一个简单的问题,我想知道如何以及是否可以使if语句检查“ hello”,而不管它是否写成hello,HELLO,HELLO等。 this is my code here don't worry all the other variables are being taken care of by another library (discord's to be exact) Thanks 这是我的代码,请不要担心其他库正在处理所有其他变量(确切地说是不一致的),谢谢

bot.on('message', function(message){
  if(message.content == "hello")
  {
    message.reply("hello");
  }

in javascript you can use .toUpperCase() at the end of any string variable or litteral 在javascript中,您可以在任何字符串变量或乱码结尾处使用.toUpperCase()

在此处输入图片说明

or same goes for .toLowerCase() so first you need to convert to upper case or lower case then compare .toLowerCase()相同,因此首先您需要转换为大写或小写,然后进行比较

if (message.content.toUpperCase()==="HELLO")

or 要么

if (message.content.toLowerCase()==="hello")

this is the full code 这是完整的代码

bot.on('message', function(message){
  if(message.content.toLowerCase() === "hello")
  {
    message.reply("hello");
  }

You can use .toUpperCase() 您可以使用.toUpperCase()

bot.on('message', function(message){
  if(message.content.toUpperCase() == "HELLO")
  {
    message.reply("hello");
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM