How do I add a character at a specific position in a string?

后端 未结 2 1534
不知归路
不知归路 2020-12-30 11:52

I am using Notepad++ and want to insert a character at a specific position in a string using regular expression replacement.

What would the expressions be to, say, i

相关标签:
2条回答
  • 2020-12-30 12:15

    If you want to add a character after the sixth character, simple use the search

    ^(.{6})
    

    and the replacement

    $1,
    

    (Example inserts a ,)

    technically spoken this will replace the first 6 characters of every line with MatchGroup 1 (backreference $1) followed by a comma.

    0 讨论(0)
  • 2020-12-30 12:31

    Also using Vanilla Javascript

    var str = 'USDYEN'
    // add a / in between currencies
    // var newStr = str.slice(0, 3) + ' / ' + str.slice(3)
    
    // var newStr = str.slice(3) // removes the first 3 chars
    // var newStr = str.slice(0,3) // removes the last 3 chars
    var newStr = str.slice(0,3) + ' / ' + str.slice(3) // removes the first 3 and adds the last 3
    
    console.log(newStr)
    

    More information how to use slice()

    https://www.w3schools.com/jsref/jsref_slice_string.asp

    0 讨论(0)
提交回复
热议问题