如何在 javascript 中的特定子字符串之后获取子字符串
How to get substring after specific substring in javascript
我正在制作一个自动回答琐事问题的机器人。我被困在这一点上。我有问题的字符串,我想将它从包含琐事的所有问题的字符串中分离出来。
在这个例子中,我有带有问题的字符串:
var questions=`This was the first 3-D film*Bwana Devil
This was the first cartoon talking picture*Steamboat Willie
This was the sequel to "Star Wars"*The Empire Strikes Back`
我有一个变量
var str="This was the first cartoon talking picture"
而且我需要在有问题的字符串中找到str的字符串,得到*后面的部分,所以
"Steamboat Willie"
.
我真的不知道该怎么做。
for( int i =0; i < questions.lenght;i++)
{
var string = questions.split("*")[i].substring(0,"\n");
}
这样的东西行得通吗?
假设您的字符串格式为在每个问题和答案对之间使用换行符:
1。按每个换行符拆分整个字符串:
var questionAnswerStringPairs = string.split('\n');
这将创建一组问题和答案字符串。每个答案将以“*”分隔。
2。映射结果数组,并创建匹配对象:
// This will store our data.
var questionAnswerPairs = {};
// Let's go through each string from step one and add the question and
// its answer to our object.
questionAnswerStringPairs.forEach( function(pair) {
// split the string at the *
pair = pair.split('*');
// pair is now an array, the first element is the question string
// the second element is the answer. Let's set those!
var question = pair[0];
var answer = pair[1];
// now let's store that info in the object.
questionAnswerPairs[question] = answer;
});
现在,您可以这样做:
questionAnswerPairs['question i want the answer to']
,或这样:questionAnswerPairs[stringObjectWithQuestion]
,你会得到你的答案!
先拆分问题,再筛选,最后得出答案
var questions="This was the first 3-D film*Bwana Devil\nThis was the first cartoon talking picture*Steamboat Willie\nThis was the sequel to \"Star Wars\"*The Empire Strikes Back"
var str="This was the first cartoon talking picture"
var answers = questions
.split("\n")
.filter(question => question.indexOf(str) > -1)
.map(question => question.substring(question.indexOf(str)+str.length+1))
alert(answers.join(","))
我正在制作一个自动回答琐事问题的机器人。我被困在这一点上。我有问题的字符串,我想将它从包含琐事的所有问题的字符串中分离出来。
在这个例子中,我有带有问题的字符串:
var questions=`This was the first 3-D film*Bwana Devil
This was the first cartoon talking picture*Steamboat Willie
This was the sequel to "Star Wars"*The Empire Strikes Back`
我有一个变量
var str="This was the first cartoon talking picture"
而且我需要在有问题的字符串中找到str的字符串,得到*后面的部分,所以
"Steamboat Willie"
.
我真的不知道该怎么做。
for( int i =0; i < questions.lenght;i++)
{
var string = questions.split("*")[i].substring(0,"\n");
}
这样的东西行得通吗?
假设您的字符串格式为在每个问题和答案对之间使用换行符:
1。按每个换行符拆分整个字符串:
var questionAnswerStringPairs = string.split('\n');
这将创建一组问题和答案字符串。每个答案将以“*”分隔。
2。映射结果数组,并创建匹配对象:
// This will store our data.
var questionAnswerPairs = {};
// Let's go through each string from step one and add the question and
// its answer to our object.
questionAnswerStringPairs.forEach( function(pair) {
// split the string at the *
pair = pair.split('*');
// pair is now an array, the first element is the question string
// the second element is the answer. Let's set those!
var question = pair[0];
var answer = pair[1];
// now let's store that info in the object.
questionAnswerPairs[question] = answer;
});
现在,您可以这样做:
questionAnswerPairs['question i want the answer to']
,或这样:questionAnswerPairs[stringObjectWithQuestion]
,你会得到你的答案!
先拆分问题,再筛选,最后得出答案
var questions="This was the first 3-D film*Bwana Devil\nThis was the first cartoon talking picture*Steamboat Willie\nThis was the sequel to \"Star Wars\"*The Empire Strikes Back"
var str="This was the first cartoon talking picture"
var answers = questions
.split("\n")
.filter(question => question.indexOf(str) > -1)
.map(question => question.substring(question.indexOf(str)+str.length+1))
alert(answers.join(","))