将字符串大写字母移到最前面,保持相对顺序-JavaScript
我们需要编写一个JavaScript函数,该函数接受带有大写和小写字母的字符串。该函数应该返回一个字符串,所有大写字母都移到该字符串的前面。
例如:如果输入字符串是-
const str = 'heLLO woRlD';
那么输出应该是-
const output = 'LLORDhe wol';
示例
以下是代码-
const str = 'heLLO woRlD';
const moveCapitalToFront = (str = '') => {
let capitalIndex = 0;
const newStrArr = [];
for(let i = 0; i < str.length; i++){
if(str[i] !== str[i].toLowerCase()){
newStrArr.splice(capitalIndex, 0, str[i]);
capitalIndex++;
}else{
newStrArr.push(str[i]);
};
};
return newStrArr.join('');
};
console.log(moveCapitalToFront(str));输出结果
以下是控制台中的输出-
LLORDhe wol
热门推荐
10 离别简短的祝福语大全
11 夸赞老师祝福语搞笑简短
12 送给老师中秋祝福语简短
13 新年简短的祝福语爱情
14 2026年春节祝福语简短
15 老爸生日暴富祝福语简短
16 新婚迎宾海报祝福语简短
17 祝寿时祝福语简短精辟
18 美好的成语祝福语简短