WPF treats a single underscore as a mnemonic in the content
of a Button
However, it is likely that the content will need to contain an underscore.
The content is defined by the user, and there is nothing to stop them having multiple underscores, sequentially or not. EG
This_Is It for the__moment and this is three___of the things
If I assign the above nonsense string to a Button.Content, it will treat the single underscore as a mnemonic and will result in ThisIs It for the__moment and this is three___of the things
(note the _ is missing and I now have ThisIs as one word). What I want it to update it This__Is It for the__moment and this is three___of the things
(note it's now double underscore, but the other occurrences of the underscore remain unchanged).
This is what I have, it's just so clunky (although it works).
static void Main(string[] args)
{
Console.WriteLine(Other("This_Is It for the__moment and this is three___of the things"));
Console.ReadKey(); // result is This__Is It for the__moment and this is three___of the things (note the double __ after the first word This)
}
static string Other(string content)
{
List<int> insertPoints = new List<int>();
for (int i = 0; i < content.Length; i++)
{
char current = content[i];
if (content[i] == '_' && content[i + 1] != '_')
{
if (i - 1 >= 0)
if (content[i - 1] == '_')
continue;
insertPoints.Add(i);
}
}
foreach (var item in insertPoints)
{
content = content.Insert(item, "_");
}
return content;
}
My question is, would there be less code with a RegEx?
string.Replace
not work here? It is because you are using ReadKey?String.Replace()
- and seems to be more efficient too... (?)