-2

So i'm trying to strip data from a string because I have in WPF a "preset" input which looks like __,___, now a user must input something like 30,589, but when a user just gives in 5 or 50, it needs to strip the rest (keeping the ,) to propperly make a float of the input value. The code that I have right now looks like this;

if (inp_km.Text == "__,___")
{
    team_results.results[inp_tour_part.SelectedIndex].km = 
        float.Parse("00,000", 
                    NumberStyles.AllowDecimalPoint, 
                    CultureInfo.GetCultureInfo("nl-NL")); // Give the new value
}
else
{
    team_results.results[inp_tour_part.SelectedIndex].km = 
        float.Parse(inp_km.Text, 
                    NumberStyles.AllowDecimalPoint, 
                    CultureInfo.GetCultureInfo("nl-NL")); // Give the new value
}

But this code just check wether the input is left blank or not... Could someone help me out?

Edit

So I've included a screen, this is the input lay-out a user gets;

inputs

Os you can see, the inputs are 'pre-filled', the content of such an input is a "string", so, let's say, I type into the first input just 5;

filled input

Then the value (retreived in C# by input_name.Text) is 5_:__, but that's a "wrong" value and you can't fill in such things, how could I check if there still is a : or _ in the input.

Also, the bottom input is the same, but then it needs to be filled in completely.

Robin
  • 1,567
  • 3
  • 25
  • 67
  • It would be awesome if you could provide a [mcve] (console app) with at least 6 sample inputs and expected results for each of those sample inputs. – mjwills Jul 30 '18 at 21:52
  • 1
    Are you using https://stackoverflow.com/a/1103822/34092 for the preset? – mjwills Jul 30 '18 at 21:53
  • Not using that SO link for the preset (mask). but I'm having trouble 'filtering' stuff out of the inputs. I also have time (HH:II) which has __:__ as mask, but when I get the input, and just fill in `5`, it returns `5_:__` I'm using `Extended.Wpf.Toolkit` (https://github.com/xceedsoftware/wpftoolkit ) – Robin Jul 30 '18 at 21:58
  • Could you elaborate what exactly you want done? I don't understand this part at all: _but when a user just gives in 5 or 50, it needs to strip the rest (keeping the ,) to propperly make a float of the input value._ – Sach Jul 30 '18 at 22:05
  • I don't understand the default value setting part. Why not just set it to `0,0`? Why are you parsing a hard-coded string to get a float? – Rufus L Jul 30 '18 at 22:07
  • @Sach I've updated my question. RufusL that was a mistake from me, my bad – Robin Jul 30 '18 at 22:10
  • So what you really want to do is see if the input is in either `12,345` format or `12:345`? – Sach Jul 30 '18 at 22:18
  • Yes, that's the main goal, but one input must be `12,345` and the other must be `12:34`. – Robin Jul 30 '18 at 22:19
  • Look at [`string.Contains`](https://msdn.microsoft.com/en-us/library/dy85x1sa(v=vs.110).aspx) and [`string.Replace`](https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx) as well as [`double.TryParse`](https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx) to solve this. – Rufus L Jul 30 '18 at 22:19

1 Answers1

2

So you want to check either the input is in one of the two forms: 12,345 or 12:34.

This can be done using Regex very easily.

static void Main(string[] args)
{
    var inputComma = "12,345";
    var inputColon = "98:76";
    Regex regexComma = new Regex(@"^\d{2},\d{3}$");
    Regex regexColon = new Regex(@"^\d{2}:\d{2}$");

    var matchComma = regexComma.Match(inputComma);
    if (matchComma.Success)
    {                
        Console.WriteLine(inputComma);
    }

    Console.WriteLine();

    var matchColon = regexColon.Match(inputColon);
    if (matchColon.Success)
    {
        Console.WriteLine(inputColon);
    }

    Console.ReadLine();
}

NOTE:

You haven't quite clarified the valid formats for your input. The above will evaluate to true strictly for 12,345 format if commas are present (i.e., two digits followed by a comma followed by three digits), and for colon, only numbers of the format 12:34 (two digits before and after the colon) only.

You might want to modify your Regex based on your exact criteria.

Sach
  • 10,091
  • 8
  • 47
  • 84
  • It checks for the REGEX here, but it always returns for the colon `00:00` and for the comma `00,000` – Robin Jul 31 '18 at 10:02
  • If I try the program you gave me, it works just fine, this is the code that I'm using; https://gist.github.com/RosiersRobin/dc7992defb0e90b922f200eb34a85a47 – Robin Jul 31 '18 at 10:17
  • I'm sorry I can't debug the entire code for you. Just use your debugger and see the value of `inp_stop_kp.Text` before the `Regex` is executed. As my example shows, if the value is in the expected format it should succeed. – Sach Jul 31 '18 at 16:39
  • It returns 00:00 the `inp_stop_kp.Text` which is really weird – Robin Jul 31 '18 at 16:48
  • In the `Inp_stop_kp_KeyDown()` event you call `LoadResults()` which in turn calls `LoadDataInInputs()`, and in there I can see some value is assigned to `inp_stop_kp.Text`. Likely that's where your value is reset. – Sach Jul 31 '18 at 16:54