Javascript 获得完整字符串的方法?

Javascript 获得完整字符串的方法?,javascript,string,substr,Javascript,String,Substr,“用户输入”是用户输入的内容 用户会说你好\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 例如:我们将使用Hello World var input = UserInput; // Let's say the user inputs hello world if(input == "hello") { var cut = input.substr(6) console.log(cut) } 用户正在输入“hello wo


“用户输入”是用户输入的内容


用户会说你好\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
例如:我们将使用Hello World

    var input = UserInput;
    // Let's say the user inputs hello world
    if(input == "hello") {
      var cut = input.substr(6)
      console.log(cut)
    }
用户正在输入“hello world”,但if语句不会接收到该信息


我的目标是从if语句中获取用户输入,但要有它,这样我就可以将他们所说的部分内容替换为substr

使用
string.indexOf
查看一个字符串是否包含另一个字符串

var input = UserInput;
// Let's say the user inputs hello world
if ( input.indexOf( "hello" ) != -1 ) {
    var cut = input.substr(6)
    console.log(cut)
}

请注意,使用这种方式,它区分大小写

您也可以拆分输入。。。字符串和数组的乐趣

//Lets say you have this string...
var input = "Hello world let me go back to bed"

//Split into an array
var eachWord = input.split(" ") //["Hello", "world", "let".....]

//Get first word
var firstWord = input[0];//"Hello"

//Get rest of sentence
var theRest = input.splice(1, input.length); //["world", "let", "me"...]

//put this into a string
theRest = theRest.join(" ") //"world let me...."
现在你可以随心所欲了。此外,在检查字符串时,将其设置为小写或大写也很重要

(firstWord.toLowerCase === "hello")

如果说“世界你好”@Peter-它仍然是一样的,字符串包含“你好”?“你好世界”=>“世界”,“你好世界”=>“你好”@Peter-我不知道这是什么意思?哈哈哈,对不起,我要说的是“你好世界”会给他“世界”,“世界你好”会给他“你好”。他想得到“他们所说的话的substr部分”使用input.toLowerCase()=“你好”