我有两个这样的领域:
标题在最后一个字之前没有空格。另一个有一个空格,因此这两个标题不被认为是相同的标题。
在我的应用程序中,我想在单词后面用空格或非空格来表示it's the same titles。
请问我怎么做?
发布于 2018-10-01 09:57:33
const title1 = "DisneyLand-Paris";
const title2 = "DisneyLand-Paris ";
const trimmed = title2.trim();
console.log(title1 === title2); // false
console.log(title1 === trimmed); // true
所以你可以:
// Trim and compare both strings, and report the result.
function compareTitles(title1, title2) {
if (title1.trim() === title2.trim()) {
console.log("It's the same titles!");
}
}
compareTitles("DisneyLand-Paris", "DisneyLand-Paris "); // It's the same titles!
compareTitles("DisneyLand-Paris ", "DisneyLand-Paris"); // It's the same titles!
发布于 2018-10-01 10:00:56
您需要使用trim()方法
const string1 = "DisneyLand-Paris";
const string2 = "DisneyLand-Paris ";
const string3 = "";
if(string1 === string2.trim()){
string3 = `it's the same titles ` ; //here you have to use template literals because of it’s
console.log(string3);
}https://stackoverflow.com/questions/52588533
复制相似问题