Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript swap characters in string

Let's say I have a string like "abcabcabc" and I want the positions of 'a's and 'b's swapped so that I end up with "bacbacbac". What is the most elegant solution for this? (For the sake of this question I hereby define 'elegant' as fast and readable.)

I came up with

"abcabcabc".replace( /[ab]/g, function( c ){ return { 'a': 'b', 'b': 'a' }[ c ] } )

Which I neither regard fast nor readable. But now I wonder if there is a better way of doing it?

EDIT: The characters can be at any position. So the answer should hold for "xyza123buvwa456" (would be then "xyzb123auvwb456", too.

EDIT2: "swap" seems to be the wrong word. Replace all of a with b and all of b with a while both are single characters.


I throw in a couple of other ones:

"abcabcabc".replace( 'a', '_' ).replace( 'b','a' ).replace( '_', 'b' )

"abcabcabc".replace( /[ab]/g, function( c ){ return "ba".charAt( c.charCodeAt()-'a'.charCodeAt() ); } )

"abcabcabc".replace( /[ab]/g, function( c ){ return "ab".charAt( "ba".indexOf( c ) ) } )

I ended up using a modified version of Mark C.'s Answer:

"abcabcabc".replace( /[ab]/g, c => c == 'a' ? 'b' : 'a' )
like image 385
Scheintod Avatar asked Feb 01 '18 20:02

Scheintod


People also ask

How do I swap characters in a string?

As we know that Object of String in Java are immutable (i.e. we cannot perform any changes once its created). To do modifications on string stored in a String object, we copy it to a character array, StringBuffer, etc and do modifications on the copy object.

How do you interchange strings in JavaScript?

Alternative swapping methods Explanation: a=b assigns the old value of b to a and yelds it, therefore [a, a=b] will be [a, b] the [0] operator yelds the first element of the array, which is a , so now b = [a,b][0] turns into b = a.

How do you replace a single character in a string JavaScript?

Javascript strings are immutable, they cannot be modified "in place" so you cannot modify a single character. in fact every occurence of the same string is ONE object.

How do you replace letters in JavaScript?

You can use the JavaScript replace() method to replace the occurrence of any character in a string. However, the replace() will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global ( g ) modifier.


3 Answers

Try this :

str.replace(/[ab]/g, function($1) { return $1 === 'a' ? 'b' : 'a' })

example:

console.log("abcabcabc".replace(/[ab]/g, function($1) { return $1 === 'a' ? 'b' : 'a' }))
like image 108
Mark C. Avatar answered Oct 16 '22 10:10

Mark C.


You can swap them (if they're always adjacent) by using a split/join combo:

console.log("abcabcabc".split("ab").join("ba"))

And in keeping with the split/join method, here's a way to swap them as individual characters (although it becomes significantly less readable):

console.log("aabbccaabbcc".split("a").map(s => s.split("b").join("a")).join("b"));
like image 29
CRice Avatar answered Oct 16 '22 10:10

CRice


replace function accepts a string as the second parameter.

"abcabcabc".replace( /ab/g, "ba")

like image 45
Leon Plata Avatar answered Oct 16 '22 12:10

Leon Plata