如何删除子字符串的空格

How to remove whitespace of a substring

提问人:Hi - P 提问时间:9/24/2023 最后编辑:Hi - P 更新时间:9/24/2023 访问量:72

问:

我正在进行函数式编程练习,我被要求用连字符替换字符串中的所有空格。如果我有一个字符串,例如 ,输出应该是 。This is a titlethis-is-a-title

现在,所有情况都像之前描述的那样工作,但我有一个边缘情况字符串,如下所示: .我想去掉前面的空格,以便我的最终输出是当我有一个没有冗余空格的初始字符串时的等效输出。使用该方法,我能够轻松地做到这一点。我该如何处理这种边缘情况? Peter Is ComingComingpeter-is-comingPeter is ComingPetertrim

注意:其中一个限制是不应使用该方法。replace

谢谢。

我的代码:

function urlSlug(title) {

  const url = title.trim().toLowerCase();
  console.log(url);

  const splitURL = url.split("");
  // console.log(splitURL);

  const urlArr = [];

  const filtered = splitURL.filter(val => {
    if (/\s/.test(val)) {
      
      urlArr.push("-");
    }
    
    else {
      urlArr.push(val);
    }
  });

  return console.log(urlArr.join(""));

}

urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"); // a-mind-needs-books-like-a-sword-needs-a-whetstone
urlSlug("Hold The Door"); // hold-the-door 
urlSlug(" Peter is  Coming"); // peter-is--coming 

// The last output is what I get but not what I want to achieve.

更新

感谢所有回答的人。我最终使用了以下代码与我的原始代码相关,它通过了边缘情况:


function urlSlug(title) {

  const url = title.trim().toLowerCase();
  console.log(url);

  const splitURL = url.split(/\s+/);
  // console.log(splitURL);

  const urlArr = [];

  const filtered = splitURL.filter(val => {
    if (/\s/.test(val)) {
      
      urlArr.push("-");
    }
    
    else {
      urlArr.push(val);
    }
  });

  return console.log(urlArr.join("-"));

}

javascript 字符串 修剪 删除空格

评论


答:

1赞 Mamun 9/24/2023 #1

该条件不考虑单词之间的连续空格字符。if (/\s/.test(val))

您可以尝试以下方法:

function urlSlug(title) {
  //remove leading and trailing spaces
  //convert to lowercase
  const trimmedTitle = title.trim().toLowerCase();

  //split words array
  const words = trimmedTitle.split(/\s+/);

  //join the words with hyphens
  const slug = words.join("-");

  return slug;
}

console.log(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone")); //a-mind-needs-books-like-a-sword-needs-a-whetstone
console.log(urlSlug("Hold The Door")); //hold-the-door 
console.log(urlSlug(" Peter is  Coming")); //peter-is-coming

评论

1赞 Hi - P 9/24/2023
这确实有效。谢谢。似乎我毕竟不需要过滤方法。
0赞 Mamun 9/24/2023
@Hi-P,是的,可以解决问题:)join()
0赞 William Kliemann 9/24/2023 #2

若要解决此问题,可以使用 split 方法将字符串分解为单词,然后使用 join 将它们与连字符连接起来。这样,多余的空格就会被自动删除。

    function urlSlug(title) {
      const url = title.trim().toLowerCase();
      const splitURL = url.split(/\s+/);
      return splitURL.join("-");
    }

    console.log(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone")); // a-mind-needs-books-like-a-sword-needs-a-whetstone
console.log(urlSlug("Hold The Door")); // hold-the-door
console.log(urlSlug(" Peter is  Coming")); // peter-is-coming

在这里,split 函数中的 /\s+/ 意味着它将在任意数量的空格处断开字符串,从而删除多余的空格。然后 join 用连字符连接单词,解决您的边缘情况。

2赞 ewhalenkamp 9/24/2023 #3

您可以创建一个新变量 previousWhiteSpace,用于跟踪 .filter() 的上一次迭代是否导致空格。

使用您当前的实现,这不需要太多更改(注意):previousWhiteSpace

function urlSlug(title) {
  const url = title.trim().toLowerCase();
  console.log(url);

  const splitURL = url.split("");
  // console.log(splitURL);

  const urlArr = [];

  let previousWhiteSpace = false;

  const filtered = splitURL.filter(val => {
    if (/\s/.test(val)) {
      // after making sure val is whitespace, pass harmlessly if previous val was whitespace
      if (!previousWhiteSpace) {
        urlArr.push("-");
        previousWhiteSpace = true;
      }
    }
    else {
      urlArr.push(val);
      previousWhiteSpace = false;
    }
  });

  return console.log(urlArr.join(""));
}

urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"); // a-mind-needs-books-like-a-sword-needs-a-whetstone
urlSlug("Hold The Door"); // hold-the-door 
urlSlug(" Peter is  Coming"); // peter-is-coming 

但是,这可以通过使用空格字符 (\s) 而不是 “” 拆分 url 来大大简化,从而节省了使用过滤器的额外迭代。

例:

function urlSlug(title) {
  const url = title.trim().toLowerCase();

  // whitespace regex to match every whitespace character
  const urlArr = url.split(/\s+/);

  return console.log(urlArr.join("-"));
}

urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"); // a-mind-needs-books-like-a-sword-needs-a-whetstone
urlSlug("Hold The Door"); // hold-the-door 
urlSlug(" Peter is  Coming"); // peter-is-coming 

编辑:因为它是Javascript,为什么不更进一步呢?

单线解决方案:

const urlSlug = (title) => console.log(title.trim().toLowerCase().split(/\s+/).join("-"));

urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"); // a-mind-needs-books-like-a-sword-needs-a-whetstone
urlSlug("Hold The Door"); // hold-the-door 
urlSlug(" Peter is  Coming"); // peter-is-coming 

评论

1赞 ewhalenkamp 9/24/2023
单线解决方案带来乐趣:const urlSlug = (title) => console.log(title.trim().toLowerCase().split(/\s+/).join("-"));
0赞 Tim Biegeleisen 9/24/2023
这条评论是最好的答案,应该是你的答案+1。