我正在尝试编写一个程序,从字符串中删除每个元音的“第一个实例”。 “敏捷的棕色狐狸跳过了懒狗” 变成 “棕色狐狸跳过了 lzy 狗”
这是我之前的代码的样子(下一篇评论)
str = "the quick brown fox jumps over the lazy dog"
letters = ['a', 'e', 'i', 'o', 'u']
i = 0
j = 0
strArr = str.split('') // converts word to string
vowel = letters[i]
// replaceVowel = str.replace(letters.indexOf(i), '');
while (i < str.length || j == 5)
if (strArr[i] == letters[j] ) {
console.log('I found ' + letters[j])
i = 0
j++
}
else if (strArr[i] !== letters[j]){
i++
}
strArr.join()
console.log(str);
我的想法是使用
split
将字符串转换为数组,然后将字符串的索引与字母数组 [aeiou] 进行比较并检查第一个元音。 第一个“a”被替换为空格,然后检查下一个元音(“e”)。 这种情况会发生直到 j=5,因为那时循环将在检查“u”后退出
然后我将代码更改为:str = "the quick brown fox jumps over the lazy dog"
letters = ['a', 'e', 'i', 'o', 'u']
index = 0
j = 0
strArr = str.split('') // converts word to string
vowel = letters[index]
while (index < strArr.length && j !== 5)
// Where j references the vowels
if (strArr[index] == letters[j]) {
strArr.splice(index, '')
index = 0
j++
// j++ to cycle through vowels after the first
// instance has been repaced
}
else if (strArr[index] !== letters[j]){
index++
// Cycle through the string until you find a vowel
}
else if (strArr[index] == str.length && j!== 6) {
index = 0
j++
// If you hit the end of the string and couldn't find
// a vowel, move onto the next one
}
strArr.join()
console.log(str);
当我运行它时什么也没有发生。 我想知道我的逻辑是否有问题?
如果还有更简单的方法,请告诉我。
非常感谢您的指导!
查找索引并使用
String#slice()
来剪切结果字符串中的每个元音的第一个。它。
const str = 'the quick brown fox jumps over the lazy dog';
let result = str;
for (const vowel of ['a', 'e', 'i', 'o', 'u']) {
const vowelIndex = result.indexOf(vowel);
result = result.slice(0, vowelIndex) + result.slice(vowelIndex + 1);
}
console.log(result);
// th qck brwn fox jumps over the lzy dog
另一种解决方案是迭代字符串的每个字符,并对照
Set
元音进行检查,如果找到则删除元音,否则将该字符添加到结果字符串中。这里使用三元对
Set.delete()
如果值在 Set 中则返回 true,否则返回 false。
const str = 'the quick brown fox jumps over the lazy dog';
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
let result = '';
for (const char of str) {
result += vowels.delete(char) ? '' : char;
}
console.log(result);
// th qck brwn fox jumps over the lzy dog
原答案
replace()
调用来实现此目的。 (请注意,这在
i
中留下了唯一的 quick
,因为 u
是该单词中的第一个元音)。
const str = 'the quick brown fox jumps over the lazy dog';
const result = str.replace(/(\b[^aeiou]*)[aeiou](\w*\b)/gi, '$1$2');
console.log(result);
// th qick brwn fx jmps ver th lzy dg
function getIndex(string, expression) {
let position = expression.exec(string);
if (position === null) return null;
return position.index;
}
function getVowelIndex(string) {
return getIndex(string, /[aeiou]/i);
}
let testCases = [
"Something has gone wrong",
"Or not",
"Breaking news",
"pfft!",
"brkn leg",
"I can't dance"
];
for (let testCase of testCases) {
console.log(testCase + ": ", getVowelIndex(testCase));
}
使用
.exec()
我们可以获得索引。
现在,让我们删除它:
function getIndex(string, expression) {
let position = expression.exec(string);
if (position === null) return null;
return position.index;
}
function getVowelIndex(string) {
return getIndex(string, /[aeiou]/i);
}
let testCases = [
"Something has gone wrong",
"Or not",
"Breaking news",
"pfft!",
"brkn leg",
"I can't dance"
];
for (let testCase of testCases) {
let result = testCase;
let position = getVowelIndex(testCase);
if (position !== null) result = result.substring(0, position) + result.substring(position + 1);
console.log(testCase + ": ", result);
}
map
遍历数组。
要删除元音,请将字符串转换为数组,然后使用findIndex
查找
vowels
包含的第一个字符。然后,删除该项目。
const str = `the quick brown fox jumps over the lazy dog`
const vowels = ['a', 'e', 'i', 'o', 'u']
const result = str.split(' ').map(e => {
const arr = [...e]
return arr.splice(arr.findIndex(c => vowels.includes(c)), 1), arr.join('')
})
console.log(result.join(' '))