javascript - Jquery - Add character after the first character of a string? - Stack Overflow

Say for example I havevar input = "C\\Program Files\\Need for Speed";var output = do_it(i

Say for example I have

var input = "C\\\\Program Files\\\\Need for Speed";

var output = do_it(input, ':'); 

Now, I would like output to have the value below :

 C:\\\\Program Files\\\\Need for Speed

I need to add a character to the given string just after the first character. How can I achieve that using javascript or jquery ?

Thanks in advance

Say for example I have

var input = "C\\\\Program Files\\\\Need for Speed";

var output = do_it(input, ':'); 

Now, I would like output to have the value below :

 C:\\\\Program Files\\\\Need for Speed

I need to add a character to the given string just after the first character. How can I achieve that using javascript or jquery ?

Thanks in advance

Share Improve this question edited May 12, 2014 at 20:15 deweyredman 1,4501 gold badge9 silver badges12 bronze badges asked May 12, 2014 at 20:08 Shika Taga NaiShika Taga Nai 731 gold badge3 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

It's probably not the most efficient way, but I would do something like:

(note: this is just pseudocode)

var output = input[0] + ":" + input.substr(1, input.length);

you can use this like

String.prototype.addAt = function (index, character) {
        return this.substr(0, index - 1) + character + this.substr(index-1 + character.length-1);
    }
    var input = "C\\Program Files\\Need for Speed";
    var result = input.addAt(2, ':');

Heres one way of doing it:

Fiddle: http://jsfiddle/FjfB9/

var input = "C\\Program Files\\Need for Speed"

var do_it = function(str, char) {
    var str = str.split(''),
        temp = str.shift()

    str.unshift(temp, char)
    return str.join('')
}

console.log(do_it(input, ":"))

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信