to UpperCase certain char-index in a string Javascript - Stack Overflow

ok I want do have a jscript below$(document).ready(function(){var str = 'post'; the subject

ok I want do have a jscript below

$(document).ready(function(){
    var str = 'post'; //the subject string
    var arr =[0,2]; //to uppercase character index 0 and 2

    str = str.split("");
    for(var i = 0; i < str.length; i++){
        if($.inArray(i,arr)){
            str[i] = str[i].toUpperCase();
        }
    }
    str = str.join('');
    alert(str);
    //the result must be PoSt
});

and you may see it running here

/ ​

now what I want there is to provide a subject string and an array.

the subject string is the string to be process and the array contains numerical values that will represent the character index in the string to uppercase.

did i miss something with my script that's why I get undersirable results?

ok I want do have a jscript below

$(document).ready(function(){
    var str = 'post'; //the subject string
    var arr =[0,2]; //to uppercase character index 0 and 2

    str = str.split("");
    for(var i = 0; i < str.length; i++){
        if($.inArray(i,arr)){
            str[i] = str[i].toUpperCase();
        }
    }
    str = str.join('');
    alert(str);
    //the result must be PoSt
});

and you may see it running here

http://jsfiddle/laupkram/WfUUp/ ​

now what I want there is to provide a subject string and an array.

the subject string is the string to be process and the array contains numerical values that will represent the character index in the string to uppercase.

did i miss something with my script that's why I get undersirable results?

Share Improve this question asked Sep 5, 2012 at 9:01 NetoricaNetorica 19.4k19 gold badges77 silver badges109 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 3

Check the docs for $.inArray . It returns the index of the element that was found or -1 if not found.

$(document).ready(function(){
    var str = 'post'; //the subject string
    var arr =[0,2]; //to uppercase character index 0 and 2

    str = str.split("");
    for(var i = 0; i < str.length; i++){
        //CHANGE HERE
        if($.inArray(i,arr) != -1){
                          //^^ change this
            str[i] = str[i].toUpperCase();
        }
    }
    str = str.join('');
    alert(str);
    //the result must be PoSt
});

$(document).ready(function() {
    var str = 'post'; //the subject string
    var arr = [0, 2]; //to uppercase character index 0 and 2
    str = str.split("");
    for (var i = 0; i < arr.length; i++) {
        if (str[arr[i]]) {
            str[arr[i]] = str[arr[i]].toUpperCase();
        }
    }
    str = str.join('');
    alert(str);
    //the result must be PoSt
});

2 Things that I see wrong here:

  1. No need to split the str, you can loop over it normally.

  2. You got inArray() wrongly. The below snippet from jquery api for inArray

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when > it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.

So, as inArray is returning -1 for indexes that it didn't find, you if statement is being true, and hence you are getting the result as 'PoST'.

I would re-write this as

var str = 'post'; //the subject string
var arr =[0,2]; //to uppercase character index 0 and 2

str = str.split("");
for(i = 0; i < arr.length; i++){
  str[arr[i]]=str[arr[i]].toUpperCase();

}
str = str.join('');
alert(str);

http://jsfiddle/NBBgz/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743900265a4526518.html

相关推荐

  • to UpperCase certain char-index in a string Javascript - Stack Overflow

    ok I want do have a jscript below$(document).ready(function(){var str = 'post'; the subject

    2天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信