简体   繁体   中英

How to move capital letters in front without using any regular expression/ built in function such as split,join,indexOf etc

function moveCapLetter(moveCap) {
  let upr = /[A-Z]/g;
  let ul = moveCap.match(upr);

  for (let i = 0; i < ul.length; i++) {
    let m = moveCap.length;
    let indx = moveCap.indexOf(ul[i]);
    moveCap =
      moveCap.substring(0, indx) + moveCap.substring(indx + 1, moveCap.length);
  }
  moveCap = ul.join("") + moveCap;
  return moveCap;
}

The only way I come up with is to test whether it is uppercase or lowercase after converting it into uppercase using toUpperCase (else how can we check) and concat separately.

 function moveCapLetter(str) { let upper = "", lower = ""; for (let i = 0; i < str.length; ++i) { if (str[i] === str[i].toUpperCase()) { upper += str[i]; } else lower += str[i]; } return (upper += lower); } const result = moveCapLetter("eNviRonMEnT"); console.log(result);

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