首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查两个字段的空间

问检查两个字段的空间
EN

Stack Overflow用户
提问于 2018-10-01 09:52:20
回答 2查看 67关注 0票数 1

我有两个这样的领域:

  • “迪斯尼乐园-巴黎”
  • “迪斯尼乐园-巴黎”

标题在最后一个字之前没有空格。另一个有一个空格,因此这两个标题不被认为是相同的标题。

在我的应用程序中,我想在单词后面用空格或非空格来表示it's the same titles。

请问我怎么做?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-01 09:57:33

您可以使用String.prototype.trim()

​

代码语言:javascript
复制
const title1 = "DisneyLand-Paris";
const title2 = "DisneyLand-Paris ";
const trimmed = title2.trim();

console.log(title1 === title2); // false
console.log(title1 === trimmed); // true

​

所以你可以:

​

代码语言:javascript
复制
// 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!

​

票数 3
EN

Stack Overflow用户

发布于 2018-10-01 10:00:56

您需要使用trim()方法

代码语言:javascript
复制
  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);
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52588533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档