I am developing a web application where there is a textbox. User enters a condition to show specific color for a "string". This string holds some integer value.
User specifies condition for the string value like this,
>30:"Red"
<20:"Green"
:"Yellow:
This is one condition. It is something like if,elseif,else condition. User can specify only if,else like condition like this
>60:"Blue"
:"White"
(Which means if the string value greater than 60 then get "Blue" as color for the string else "White" is the color for the string)
I have to evaluate color for the string based on the above condition entered in the textbox for each string. There are thousands of strings with values and user will enter such expression for each string.
what is the best way to solve this issue?
String input = getTextBoxInput() // get input from text box
if(input.contains("<") || input.contains("<"))
{
if(input.contains("<"))
{
int index = input.indexOf("<");
String conditionString = input.substring(index+1,input.length());
// condition number is the number being compared
int conditionNumber = conditionString.split(":")[0];
// if condition is true then get the corresponding colour
if(number < conditionNumber){setColor(conditionString.split(":")[1])}
// check if it an if else condition and do the following
else if(input.length()>1){ setColor(conditionString.split(":")[2])}
}
}
This is the basic pseudo code. You can define constants for "<" and ">" . Also you may need to do some exception handling incase of incorrect input.
please see Shunting Yard algorithm , you need to have something similar
Related
How can I check for a comma in a number?
Lets assume I have a string which represents a polynomial term that looks like this
string x = "x+1+5,54";
Now the user wants to put in and add a comma which will then be "x1+5,54,"
which is not a number anymore. How can I check this with an if ?
Something like if the last number already contains a comma don't append another one.
Use regular expression.
if (Regex.IsMatch(a, #"^((\d+,?\d*)|(\w?))([-+/]((\d+,?\d*)|(\w?)))*$"))
{
//correct
}
else
{
//incorrect
}
You'll get false, when user inputs extra comma, so you can handle it.
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;
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;
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.
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.
This question already has answers here:
Identify if a string is a number
(26 answers)
Closed 5 years ago.
I'm receiving some user input through a TextBox which is then being converted to an int so it can be used in further calculations when I click the Calculate button.
I have checked to see if the TextBox is empty when the Calculate button is clicked, if it is, then a message box appears. Now I realised I need to check to make sure it is a number being input, not a letter. I'm looking for something similar to this
if(hoursInput.Text == "" || hoursInput.Text contains "a-z")
{
\\ handle error
}
else
{
\\ continue with code
}
EDIT:
The user input is converted to an int in the else block, but I do not want the function to reach this stage of converting from string to int if the user input contains letters, which is why I want to check to see if the user input contains any letters in the if block
As mentioned, use Int32.TryParse which will return a bool of whether or not the input could be parsed to an Int32. One of the parameters is an out and will become the Int32 if the input could be parsed.
See:
https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx
if(hoursInput.Text == "" || !Int32.TryParse(hoursInput.Text, out number))
{
\\ handle error
}
else
{
\\ continue with code
}
You really don't need to check anything explicitly:
int aNumber;
if (!Int32.TryParse(hoursInput.Text, out aNumber)) {
// handle error
} else {
// handle `aNumber`
}
I am trying to figure out how to format a string to a specific pattern.
When a user is entering their employee id number, they often get confused on what is expected from them. Because they are often told that their employee id is either a 5 digit or 4 digit number depending on when they were hired.
For example, my employee id number is E004033 but for most of our systems, I just have to enter 4033 and the system will find me.
We are trying to add this to one of our custom pages. Basically what I want to do is format a string to always look like E0XXXXX
So if they enter 4033 the script will convert it to E004033, if they enter something like 0851 it will convert it to E000851 or if they enter 11027 it will convert it to E011027
Is there a way basically add padding zeros and a leading E if they are missing from the users input?
You can simply:
var formattedId = "E" + id.PadLeft(6, '0');
To remove an existing leading E(s)
var text = "E" + val.TrimStart(new[] {'E'}).PadLeft(6, '0');
Make sure the user's input is an integer, then format to 6 spaces using String.Format.
int parsedId;
bool ok = int.TryParse(id, out parsedId);
if (ok)
{
return String.Format("E{0:000000}", parsedId);
}
I'm taking a value from a textbox and converting it to decimal. But, the textbox value could be empty. So, how could I handle empty strings from the textbox?
Unfortunately I have around 50 textboxes to deal with, so answers like 'check for null with IF condition' won't help me. My code will look ugly if I use all those IF conditions.
I have this
Convert.ToDecimal(txtSample.Text)
To handle nulls, I did this
Convert.ToDecimal(txtSample.Text = string.IsNullOrEmpty(txtSample.Text) ? "0" : txtSample.Text)
But, the above code is displaying '0' in the textbox. User does not want to see '0'. Another solution is to take text box value into a variable and convert the variable like below.
string variable = txtSample.Text;
Convert.ToDecimal(variable = string.IsNullOrEmpty(variable) ? "0" : variable)
But again, I do not want to define around 50 variables. I am looking for some piece of code that handles null values during conversion without adding the extra line of code.
But, the above code is displaying '0' in the textbox. User does not want to see '0'.
This is because your statement is assigning the new value to txtSample.Text (when you do txtSample.Text = ...). Just remove the assignment:
Convert.ToDecimal(string.IsNullOrEmpty(txtSample.Text) ? "0" : txtSample.Text)
To make things easier if you have many text fields to handle, you can define an extension method :
public static string ZeroIfEmpty(this string s)
{
return string.IsNullOrEmpty(s) ? "0" : s;
}
And use it like this:
Convert.ToDecimal(txtSample.Text.ZeroIfEmpty())
You could make a function to keep from copying the code all over the place.
decimal GetTextboxValue(string textboxText)
{
return Convert.ToDecimal(string.IsNullOrEmpty(textboxText) ? "0" : textboxText);
}
and then use it like this:
GetTextboxValue(txtSample.Text);
You can create an extension method for the string as below
public static decimal ToDecimal(this string strValue)
{
decimal d;
if (decimal.TryParse(strValue, out d))
return d;
return 0;
}
Then you can just txtSample.Text.ToDecimal() in every place.