简体   繁体   中英

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

Is it possible to Find specific character from string and change its position to previous character

for example: Let us say there is say a string: Kù Iù Mù

I want output like: ùK ùI ùM

Yes of course, You can go over the string in loop char by char, look at the next position and if it is the char U want, switch the position!

 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ù', 'ù'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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