'Adding a character at the beginning of a string if it does not exist using LINQ

I have a string which I apply some LINQ methods. After applying a Replace, I would like to add a '/' character at the beginning of the resulting string if and only if it does not exists yet.

For example:

string myString = "this is a test";
string newString = myString.Replace(" " , string.Empty).AddCharacterAtBeginingIfItDoesNotExists('/');

So the resulting newString should be:

"/thisisatest"

If character '/' already exists, it is not needed to add it. For example:

string myString = "/this is a test";
string newString = myString.Replace(" " , string.Empty).AddCharacterAtBeginingIfItDoesNotExists('/');

So the resulting newString should be:

"/thisisatest"



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source