Open In App

JavaScript Program to Swap Characters in a String

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.

Example: Define a function to swap characters in a string using array manipulation.






// Define a function to swap characters in a 
// string using array manipulation
function swapCharacters(inputString, index1, index2) {
    // Convert the input string into an array of characters
    let charArray = inputString.split('');
  
    // Swap the characters at the specified indices 
    // using array destructuring
    [charArray[index1], charArray[index2]] = 
        [charArray[index2], charArray[index1]];
  
    // Join the array back into a string 
    // and return the result
    return charArray.join('');
}
  
// Original string
const originalString = "example";
  
// Swap characters at index 0 and 5
const swappedString = 
    swapCharacters(originalString, 0, 5);
  
// Output the swapped string
console.log(swappedString);

Output
lxampee

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.

Example: Define a function to swap characters in a string using string concatenation.




// Define a function to swap characters in 
// a string using string concatenation
function swapCharactersConcat(inputString, index1, index2) {
  
    // Extract characters at the specified indices
    const charAtIndex1 = inputString[index1];
    const charAtIndex2 = inputString[index2];
  
    // Create a new string with characters swapped
    const swappedString = inputString.substring(0, index1) +
        charAtIndex2 +
        inputString.substring(index1 + 1, index2) +
        charAtIndex1 +
        inputString.substring(index2 + 1);
  
    // Return the swapped string
    return swappedString;
}
  
// Original string
const originalString = "example";
  
// Swap characters at index 0 and 5
const swappedString = 
    swapCharactersConcat(originalString, 0, 5);
  
// Output the swapped string
console.log(swappedString);

Output
lxampee

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.

Example: Define a function to swap characters in a string using regular expressions




// Define a function to swap characters in a 
// string using regular expressions
function swapCharactersRegex(inputString, index1, index2) {
  
    // Create a pattern that captures substrings 
    // before, between, and after the indices
    const pattern = 
        `(.{${index1}})(.)(.{${index2 - index1 - 1}})(.)(.*)`;
  
    // Create a replacement pattern that reorders 
    // the captured groups
    const replacement = `$1$4$3$2$5`;
  
    // Use the regular expression to replace the 
    // captured substrings and swap the characters
    const swappedString = 
        inputString.replace(new RegExp(pattern), replacement);
  
    // Return the swapped string
    return swappedString;
}
  
// Original string
const originalString = "example";
  
// Swap characters at index 0 and 5
const swappedString = 
    swapCharactersRegex(originalString, 0, 5);
  
// Output the swapped string
console.log(swappedString);

Output
lxampee

Article Tags :