Insert character into string javascript
How can I insert a string at a specific index of another string? Show
Suppose I want to insert "bar " after the "foo" how can I achieve that? I thought of
asked Nov 30, 2010 at 12:40
Jiew MengJiew Meng 82.4k174 gold badges476 silver badges788 bronze badges 1 Inserting at a specific index (rather than, say, at the first space character) has to use string slicing/substring:
answered Nov 30, 2010 at 13:02
4 You could prototype your own Polyfill
Example
EDIT: Modified it to ensure that
Mr. Polywhirl 36.6k12 gold badges76 silver badges124 bronze badges answered Nov 30, 2010 at 13:03
user113716user113716 313k61 gold badges444 silver badges436 bronze badges 4 Here is a method I wrote that behaves like all other programming languages:
Reference:
answered Feb 6, 2012 at 13:34
Base33Base33 3,1392 gold badges26 silver badges30 bronze badges 2 Just make the following function:
and then use it like that:
Output: foo bar baz It behaves exactly, like the C# (Sharp) String.Insert(int startIndex, string value). NOTE: This insert function inserts the string value (third parameter) before the specified integer index (second parameter) in the string str (first parameter), and then returns the new string without changing str! answered Apr 21, 2014 at 11:28 0 UPDATE 2016: Here is another just-for-fun (but more serious!) prototype function based on one-liner
Previous (back to 2012) just-for-fun solution:
answered Nov 19, 2012 at 15:17
VisioNVisioN 140k31 gold badges274 silver badges275 bronze badges 2 This is basically doing what @Base33 is doing except I'm also giving the option of using a negative index to count from the end. Kind of like the substr method allows.
Example: Let's say you have full size images using a naming convention but can't update the data to also provide thumbnail urls.
answered Oct 24, 2014 at 14:17
Ryan OreRyan Ore 1,24716 silver badges23 bronze badges 1 If anyone is looking for a way to insert text at multiple indices in a string, try this out:
For example, you can use this to insert
answered Aug 15, 2014 at 15:25
4
The benefits of this approach are two-fold:
answered Feb 12, 2020 at 14:20
Ben AstonBen Aston 50.5k61 gold badges194 silver badges324 bronze badges 1 Given your current example you could achieve the result by either
or
but given that you can make such assumptions, you might as well skip directly to Gullen's example. In a situation where you really can't make any assumptions other than character index-based, then I really would go for a substring solution. answered Nov 30, 2010 at 12:53
David HedlundDavid Hedlund 126k31 gold badges200 silver badges219 bronze badges
https://jsfiddle.net/gaby_de_wilde/wz69nw9k/ I know this is an old thread, however, here is a really effective approach.
What's great about this is that it coerces the node content. So if this node were already on the DOM, you wouldn't need to use any query selectors or update the innerText. The changes would reflect due to its binding. Were you to need a string, simply access the node's text content property.
answered Aug 8, 2018 at 0:09
You can do it easily with regexp in one line of code
"Hello Lovely RegExp!" answered Mar 15, 2019 at 17:34
MadmadiMadmadi 1,9161 gold badge11 silver badges17 bronze badges Well, we can use both the substring and slice method.
The only problem of a substring method is that it won't work with a negative index. It's always take string index from 0th position.
answered Mar 15, 2019 at 12:43
kamalkamal 8871 gold badge9 silver badges20 bronze badges 3 You can use Regular Expressions with a dynamic pattern.
outputs:
This replaces answered Feb 15, 2013 at 5:39
InoperableInoperable 1,3895 gold badges15 silver badges32 bronze badges another solution, cut the string in 2 and put a string in between.
answered Feb 2, 2013 at 7:19
Using sliceYou can use
Splice method for StringsYou can
Applying splice() at multiple indexesThe method takes an array of arrays each element of array representing a
single
answered Mar 13, 2019 at 5:29
Maheer AliMaheer Ali 34.7k5 gold badges38 silver badges67 bronze badges 1 Take the solution. I have written this code in an easy format:
Dolly 1,98113 silver badges31 bronze badges answered Sep 24, 2020 at 3:33
I wanted to compare the method using substring and the method using slice from Base33 and user113716 respectively, to do that I wrote some code also have a look at this performance comparison, substring, slice The code I used creates huge strings and inserts the string "bar " multiple times into the huge string
The general difference in performance is marginal at best and both methods work just fine (even on strings of length ~~ 12000000)
answered Mar 14, 2019 at 13:16
PierrePierre 1541 silver badge9 bronze badges 1 Can you add to a string in JavaScript?In JavaScript, concat() is a string method that is used to concatenate strings together. The concat() method appends one or more string values to the calling string and then returns the concatenated result as a new string.
How do you input a character in JavaScript?Definition and Usage. The charAt() method returns the character at a specified index (position) in a string. The index of the first character is 0, the second 1, ...
How do I add a character to a string in typescript?“insert character in string typescript” Code Answer's. function addStr(str, index, stringToAdd){. return str. substring(0, index) + stringToAdd + str. substring(index, str. ... . let str = "This is a string";. let stringToAdd = "modyfied ";. console. log(addStr(str, 10, stringToAdd)); //outPut : "This is a modified string". How do you add a character to a string in Java?Syntax: str. insert(int position, char x); str.
|