admin管理员组文章数量:1191932
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' )
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' )
Share
Improve this question
edited Aug 17, 2020 at 21:08
Dan Knights
8,3684 gold badges27 silver badges54 bronze badges
asked Feb 1, 2018 at 20:44
ScheintodScheintod
8,1059 gold badges46 silver badges64 bronze badges
6
|
Show 1 more comment
5 Answers
Reset to default 14Try 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' }))
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"));
replace
function accepts a string as the second parameter.
"abcabcabc".replace( /ab/g, "ba")
I use this method when I need to swap long list of characters\words\strings!
The method also prevents strings already replaced to be replaced again, Example:
String = "AAABBBCCC BBB"
Replace "AAABBBCCC" with "BBB", then, "BBB" with "DDD"
Final String = "BBB DDD"
Note that, only "BBB" from the original string is replaced to "DDD"! The "BBB" replaced from "AAABBBCCC" is not re-replaced to "DDD"!
Use a direct replace using Regex:
var result = "abcabcabc".replace( /ab/g, "ba");
console.log(result);
本文标签: Javascript swap characters in stringStack Overflow
版权声明:本文标题:Javascript swap characters in string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738457413a2087830.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
a
withb
andb
witha
rather than swapping. Is that actually intentional, because it's not quite the same thing. For exampleaaab
->bbba
if you just replace, whereas if you swap you might expectbaaa
(oraaba
your rules on swapping aren't clear here) – Matt Burland Commented Feb 1, 2018 at 20:47