简体   繁体   English

使用 javascript 交换字符串中每个单词的第一个和最后一个字母

[英]swapping the first and last letters of every word in a string using javascript

Given a string of words separated by spaces, write a function that swaps the first and last letters of every word.给定一串由空格分隔的单词,编写一个 function 交换每个单词的第一个和最后一个字母。 You may assume that every word contains at least one letter, and that the string will always contain at least one word.你可以假设每个单词至少包含一个字母,并且字符串总是至少包含一个单词。 You may also assume that each string contains nothing but words and spaces, and that there are no leading, trailing, or repeated spaces.您还可以假设每个字符串只包含单词和空格,并且没有前导、尾随或重复的空格。

This is what I have so far这是我到目前为止所拥有的

function first_last(str1){
 if (str1.length <= 1) {
  return str1;
 }

  mid_char = str1.substring(1, str1.length - 1);
  return (str1.charAt(str1.length - 1)) + mid_char + str1.charAt(0);
 }
 console.log(first_last('Hello there')); //eello therH

first split the sentence to word and then swap the first and last letter首先将句子拆分为单词,然后交换第一个和最后一个字母

 const str = "This is a hello world string"; const result = str.split(" ").map(word => { const len = word.length; if (len > 1) { word = word[len-1] + word.substring(1, len-1) + word[0]; } return word; }).join(" "); console.log(result)

breaking it into statements for people who find the implementation difficult to comprehend将其分解为那些发现实施难以理解的人的陈述

const str = "This is a hello world string";

const arr = str.split(" ");

const swapWords = arr.map(word => {
  const len = word.length;
  if (len > 1) {
    word = word[len - 1] + word.substring(1, len - 1) + word[0];
  }
  return word;
});

const result = swapWords.join(" ");

console.log(result)

A bit fancier example with ES6 stuff: ES6 的一个更漂亮的例子:

 const invertFirstWithLastLetter = string => string.split(" ").map(word => { if (word.length === 1) { return word; }; word = [...word]; [word[0], word[word.length - 1]] = [word[word.length - 1], word[0]]; return word.join("").toString(); }).join(" "); console.log(invertFirstWithLastLetter("ES6 is not a crime"));

How it works:这个怎么运作:

We split the string on every space, creating an array of words.我们在每个空格上拆分字符串,创建一个单词数组。 We map the array:我们map阵列:

if the word is composed of a single letter/char only, we don't need to invert.如果单词仅由单个字母/字符组成,我们不需要反转。

If it's bigger than one character, we will spread the array literal , hence we will create another (nested) array and we are going to invert the first and last letter.如果它大于一个字符,我们将传播数组字面量,因此我们将创建另一个(嵌套)数组,我们将反转第一个和最后一个字母。 Before returning this (nested) array, we join it again (to remove , ) and we return a string out of it instead of a (nested) array.在返回这个(嵌套)数组之前,我们再次加入它(删除, )并从中返回一个字符串而不是一个(嵌套)数组。

In the end, we are left with an array with the words (with inverted first and last letter), that we will merge again, recreating the same space that we used to split at the first line of the function.最后,我们得到了一个包含单词的数组(第一个和最后一个字母倒置),我们将再次合并,重新创建我们在 function 的第一行分割的相同空间。

Is this the best approach (the most performant way) to do that?这是最好的方法(最有效的方法)吗? Probably not.可能不是。 But it might teach a lot of cool stuff.但它可能会教很多很酷的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM