簡體   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