Swapping Characters of a String in C#

Swapping characters in a string involves replacing specific characters with their counterparts while preserving the original structure. In C#, this can be accomplished using various approaches including the Select method with LINQ, character arrays, or the Replace method.

Syntax

Following is the syntax for swapping characters using LINQ's Select method −

string.Select(character => condition ? replacement : character).ToArray()

Following is the syntax for swapping using character arrays −

char[] charArray = string.ToCharArray();
// modify charArray elements
new string(charArray)

Using LINQ Select Method

The Select method processes each character individually and applies conditional logic to determine replacements. This approach is ideal for swapping two specific characters throughout the string −

using System;
using System.Linq;

public class Program {
    public static void Main() {
        string str = "PQRQP";
        
        var result = str.Select(a => a == 'P' ? 'Q' : (a == 'Q' ? 'P' : a)).ToArray();
        string swappedString = new String(result);
        
        Console.WriteLine("Original: " + str);
        Console.WriteLine("Swapped:  " + swappedString);
    }
}

The output of the above code is −

Original: PQRQP
Swapped:  QPRPQ

Using Character Array Manipulation

For swapping characters at specific positions, converting the string to a character array provides direct access to individual elements −

using System;

public class Program {
    public static void Main() {
        string str = "HELLO";
        char[] charArray = str.ToCharArray();
        
        // Swap first and last characters
        char temp = charArray[0];
        charArray[0] = charArray[charArray.Length - 1];
        charArray[charArray.Length - 1] = temp;
        
        string swappedString = new string(charArray);
        
        Console.WriteLine("Original: " + str);
        Console.WriteLine("Swapped:  " + swappedString);
    }
}

The output of the above code is −

Original: HELLO
Swapped:  OELLH

Using Replace Method for Simple Swaps

When swapping two distinct characters, the Replace method can be used with a temporary placeholder −

using System;

public class Program {
    public static void Main() {
        string str = "ABCABC";
        
        // Swap 'A' with 'C' using temporary placeholder
        string result = str.Replace('A', '*')  // A becomes *
                          .Replace('C', 'A')   // C becomes A
                          .Replace('*', 'C');  // * becomes C
        
        Console.WriteLine("Original: " + str);
        Console.WriteLine("Swapped:  " + result);
    }
}

The output of the above code is −

Original: ABCABC
Swapped:  CBACAR

Comparison

Method Use Case Performance
LINQ Select Complex character transformations Good for short strings
Character Array Position-based swapping Most efficient for large strings
Replace Method Simple character substitution Fast for two-character swaps

Conclusion

Swapping characters in C# strings can be accomplished through LINQ's Select method for conditional replacements, character arrays for position-based swapping, or the Replace method for simple substitutions. Choose the approach based on your specific swapping requirements and performance considerations.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements