I have to swap the characters in a string before sending said string to a device to show the information sent through a LCD, the method I developed is as follows:
private string StringSwap(string stringToSwap)
{
if ((stringToSwap.Length % 2).Equals(1))
{
stringToSwap += " ";
}
char[] array = stringToSwap.ToCharArray();
for (int i = 0; i < array.Length; i += 2)
{
char temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
return new string(array);
}
I want to know if there are any implementations that do this already (I feel like I am reinventing the wheel here) or, in case there is not, know how to optimize and make this code more resilient, while also making it more readable.
Also, should I make this an extension for the string
class instead of a method?