JavaScript Program to Swap Characters in a String
Last Updated :
03 Oct, 2023
In this article, We’ll explore different approaches, understand the underlying concepts of how to manipulate strings in JavaScript and perform character swaps efficiently.
There are different approaches for swapping characters in a String in JavaScript:
Approach 1: Using Array Manipulation
In this approach, we convert the input string into an array of characters, perform the character swap using array manipulation techniques, and then convert the array back into a string.
- Convert the String to an Array: We begin by splitting the input string into an array of individual characters. This allows us to directly access and modify characters using array indexing.
- Swap Characters: Using array destructuring, we swap the characters at the desired positions in the array. This approach is concise and readable.
- Convert Back to String: After swapping the characters in the array, we use the join() method to convert the modified array back into a string.
Example: Define a function to swap characters in a string using array manipulation.
Javascript
function swapCharacters(inputString, index1, index2) {
let charArray = inputString.split( '' );
[charArray[index1], charArray[index2]] =
[charArray[index2], charArray[index1]];
return charArray.join( '' );
}
const originalString = "example" ;
const swappedString =
swapCharacters(originalString, 0, 5);
console.log(swappedString);
|
Approach 2: Using String Concatenation
This approach involves breaking down the original string into substrings, rearranging them, and then concatenating them to create the swapped string.
- Extract Substrings: We use the substring() method to extract three substrings: before the first character to swap, between the two characters to swap, and after the second character to swap.
- Construct Swapped String: By rearranging these substrings and inserting the characters to swap in the desired positions, we construct the swapped string.
Example: Define a function to swap characters in a string using string concatenation.
Javascript
function swapCharactersConcat(inputString, index1, index2) {
const charAtIndex1 = inputString[index1];
const charAtIndex2 = inputString[index2];
const swappedString = inputString.substring(0, index1) +
charAtIndex2 +
inputString.substring(index1 + 1, index2) +
charAtIndex1 +
inputString.substring(index2 + 1);
return swappedString;
}
const originalString = "example" ;
const swappedString =
swapCharactersConcat(originalString, 0, 5);
console.log(swappedString);
|
Approach 3: Using Regular Expressions
Using regular expressions, we can capture the characters to be swapped and rearrange them accordingly, resulting in the desired swapped string.
- Create a Pattern: We define a regular expression pattern that captures groups of characters, including those before and after the characters to swap.
- Create a Replacement Pattern: A replacement pattern is crafted to rearrange the captured groups, effectively swapping the characters in the string.
- Apply the Regular Expression: We use the replace() method with the regular expression and the replacement pattern to perform the character swap.
Example: Define a function to swap characters in a string using regular expressions
Javascript
function swapCharactersRegex(inputString, index1, index2) {
const pattern =
`(.{${index1}})(.)(.{${index2 - index1 - 1}})(.)(.*)`;
const replacement = `$1$4$3$2$5`;
const swappedString =
inputString.replace( new RegExp(pattern), replacement);
return swappedString;
}
const originalString = "example" ;
const swappedString =
swapCharactersRegex(originalString, 0, 5);
console.log(swappedString);
|
Share your thoughts in the comments
Please Login to comment...