Javascript String setCharAt(index, ch)

Description

Javascript String setCharAt(index, ch)


String.prototype.setCharAt = function (index, ch){
    if (index > this.length || index < 0) return this;
    return this.substring(0, index) + ch + this.substring(index+1);
}

function sortLetter(str){
    for (var i = 0; i < str.length; i++)
        for (var j = i+1; j < str.length; j++)
            if (str.charCodeAt(i) > str.charCodeAt(j)){
                var t = str.charAt(i);
                str = str.setCharAt(i, str.charAt(j));
                str = str.setCharAt(j, t);
            }// w  w w  .j  a  v a  2s  .  co  m
    return str;
}

var str = 'webmaster';

console.log(str);
console.log(sortLetter(str));

Javascript String setCharAt(index, ch)

String.prototype.setCharAt = function (index, ch){
    if (index > this.length || index < 0) return this;
    return this.substring(0, index) + ch + this.substring(index+1);
}

function convert(str){
    str = ' ' + str;
    var i = 0;//from  w  ww. j  ava  2 s  . co m
    while (i < str.length){
        i++;
        if (str.charAt(i) !== ' '.charAt(0) && str.charAt(i-1) === ' '.charAt(0))
            if (str.charCodeAt(i) > 96 && str.charCodeAt(i) < 123)
                str = str.setCharAt(i, String.fromCharCode(str.charCodeAt(i) - 32));
    }
    return str.substring(1);
}

var str = 'the quick brown fox';

console.log(str);
console.log(convert(str));



PreviousNext

Related