简体   繁体   English

Javascript:替换字符串中的重复字符?

[英]Javascript: Replace repeated character in a string?

I've been solving katas on codewars and I stumbled upon a general code problem that I can't seem to find an answer to.我一直在解决 codewars 上的 katas,我偶然发现了一个我似乎无法找到答案的通用代码问题。

This is my code:这是我的代码:

function wave(str) {
   let arr = [];
   for (var i = 0; i < str.length; i++) {
       str[i].match(/\S/) && arr.push(str.replace(str[i], str[i].toUpperCase()));
   }
   return arr;
 };

(Note: Input is always lowercase.) In the case of wave("abc def"); (注意:输入总是小写。) 在wave("abc def");的情况下wave("abc def"); the code does exactly what I want it to:代码完全符合我的要求:

["Abc def", "aBc def", "abC def", "abc Def", "abc dEf", "abc deF"]

=> Function takes a string, capitalizes one letter of the string starting at str[0] , pushes the new word to arr , increments i and repeats process until i < str.length , then returns arr with all the results. => 函数接受一个字符串,将字符串中从str[0]开始的一个字母大写,将新单词推送到arr ,递增i并重复过程直到i < str.length ,然后返回arr和所有结果。

However, if I input wave("acc def");但是,如果我输入wave("acc def"); for example, only the first occurrence of the letter c will return capitalized:例如,只有第一次出现字母c才会返回大写:

["Acc def", "aCc def", "aCc def", "acc Def", "acc dEf", "acc deF"]

Question: Why does it 'jump' the second occurrence of 'c' and how can I target the second or nth occurrence of a character in a string?问题:为什么它会“跳转”第二次出现的 'c' 以及如何定位字符串中第二次或第 n 次出现的字符?

str.replace("x", "y") replaces the first occurrence of "x" in str with "y" str.replace("x", "y")str.replace("x", "y")替换str第一次出现的"x" "y"
If there are only unique characters in the string your approach works, but it will fail when there are duplicated characters like in "acc def"如果字符串中只有唯一字符,则您的方法有效,但是当出现"acc def"等重复字符时,它将失败

You could .slice() the string into three parts:您可以将.slice()字符串分成三部分:

  • The part left of i -> .slice(0, i) i -> .slice(0, i)左边的部分
  • The character at index i -> str[i]索引i -> str[i]处的字符
  • The remaining part after index i -> .slice(i + 1)索引i -> .slice(i + 1)之后的剩余部分

Modify str[i] as required and combine them back into a single string with .join("")根据需要修改str[i]并使用.join("")将它们组合回单个字符串

 function wave(str) { const result = []; for (let i = 0; i < str.length; i++) { if (!str[i].match(/\\S/)) continue; result.push([ str.slice(0, i), /* the part left of str[i] */ str[i].toUpperCase(), /* str[i] */ str.slice(i + 1) /* the remaining part right of str[i] */ ].join("")) // combine the parts in the array into a string } return result; }; console.log(JSON.stringify(wave("abc def"))); console.log(JSON.stringify(wave("acc def")));

When you are saying str.replace(str[i], str[i].toUpperCase()) , you are saying: "Replace the first occurrence of str[i] with str[i].toUpperCase() " and this is exactly what your program is doing.当你说str.replace(str[i], str[i].toUpperCase())你说: “替换第一次出现的str[i]str[i].toUpperCase()这是正是你的程序在做什么。

Consider building your new string like this:考虑像这样构建你的新字符串:

str.substring(0, i) + str[i].toUpperCase() + str.substring(i+1)

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

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