繁体   English   中英

从字符串中查找特定字符并使用 javascript、jquery 将其 position 更改为前一个字符

[英]Find specific character from string and change its position to previous character using javascript, jquery

是否可以从字符串中查找特定字符并将其 position 更改为前一个字符

例如:让我们说有一个字符串: Kù Iù Mù

我想要 output 喜欢: ùKùIùM

是的,当然,你可以 go 在循环中逐个字符地遍历字符串,看看下一个 position 如果是 U 想要的字符,切换位置!

 function switchChar(text, charToFind) { //temp variable for building result var result = ""; //loop over original string for (var i = 0; i < text.length; i++) { //chack not to jump out of array if (i + 1 < text.length) { //if next char is the char im looking for if (text[i + 1] == charToFind) { //write next char first result += charToFind; //then write original char result += text[i]; //iterate i once more to jump over next char (I appended two chars in this single cycle) i++; //if it is not the char Im looking for } else { //write it down result += text[i]; } //I am at the end } else { //write the char result += text[i]; } } return result; } console.log(switchChar('Kù Iù Mù', 'ù'))

暂无
暂无

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

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