Search code examples
javascriptjquerycharsubstr

Add string after number of characters (ONE TIME!)


I'm looking for an easy and best practice solution to add the string : after 2 characters in my string.

My string looks like this: 1100. Now I would like to have the result: 11:00.

I've found only many solutions to add : after 2 characters, but they also will add : after 00 because the solution is to add : after every 2 characters.

That's not what I want, I need only one addition of : after 2 characters.

Can you help me?


Solution

  • var str = "1100";
    
    var result= str.substring(0,2) + ":" + str.substring(2);
    
    console.log(result)
    

    DEMO