0

I've got a TimeSpan with a custom format like @"hh\:mm\:ss\.fff", and want to use the MaskedTextBox from xceed (https://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox&referringTitle=Home) to help the user to input a valid timespan.

Now I convert the FormatString-Property to an input mask like this

public string InputMask
{
  get
  {
    string mask = FormatString.Replace('h', '0');
    mask = mask.Replace('m', '0');
    mask = mask.Replace('s', '0');
    mask = mask.Replace('f', '0');
    mask = mask.Replace('d', '0');
    return mask;
  }
}

this solution looks ugly and not maintainable if the FormatString gets another format which I don't know yet. Is there a more elegant solution (e.g. with a regex replace), which replaces any letter with a 0?

Herm
  • 2,956
  • 19
  • 32

1 Answers1

2

This should do the trick:

//using System.Text.RegularExpressions;

string input = @"hh\:mm\:ss\.fff"; //i suppose it's FormatString in your case, don't know the MaskedTextBox
string output = Regex.Replace(input, "[a-zA-Z]","0");
KeyNone
  • 8,745
  • 4
  • 34
  • 51