Now i have this piece of code already from this forum, works perfectly though my needs have slightly changed 'since testing!'
The code finds 2 numbers from the string.
then tests my variable against these numbers, if below/above by more than 1 gives no matched.
Else if varied by 1 gives Variation and if between the numbers gives Matched.
Here is the code.
public static string TestMatch(string range, int comp)
{
string result = "Not Matched";
if (string.IsNullOrEmpty(range) || comp < 0)
{
result = "";
}
else
{
// removes characters from the string
// (http://stackoverflow.com/questions/4366422/how-to-remove-characters-from-a-string-using-linq)
string strDigits = new String(range.Where(Char.IsDigit).ToArray());
// creates upper and lower limits
int lower = (strDigits.Length > 0 ? Convert.ToInt16(strDigits.Substring(0,1)) : 0);
int upper = (strDigits.Length > 1 ? Convert.ToInt16(strDigits.Substring(1,1)) : lower);
// compares values
if (comp >= lower && comp <= upper)
{
result = "Matched";
}
else if (comp >= (lower - 1) && comp <= (upper + 1))
{
result = "Variation";
}
}
return result;
}
My change is that the variable string, which above is 'range' could now contain 3 or more numbers.
The good thing is the reamining code already wil match if between the 2, so rather than pulling the first 2 digits it can find for 'lower' and 'upper' I now need 'lower' and 'upper' to be the biggest/smallest number found in the string respectively. If only
single number found then will act the same as it does.
I am sure this is easy and i thought i understood the code, however making this amendment is beyond me!
@{
var input = "281379465";
var strDigits = new String(input.Where(Char.IsDigit).ToArray());
var max = (int)strDigits.Max();
var min = (int)strDigits.Min();
foreach(var d in strDigits)
{
@d @((int)d>min && (int)d<max?" inRange":(int)d==min?" is the minimum":" is the maximum") <br />
}
}
My change is that the variable string, which above is 'range' could now contain 3 or more numbers.
The good thing is the reamining code already wil match if between the 2, so rather than pulling the first 2 digits it can find for 'lower' and 'upper' I now need 'lower' and 'upper' to be the biggest/smallest number found in the string respectively. If only
single number found then will act the same as it does.
Try amending your function as follows:
public static string TestMatch(string range, int comp)
{
string result = "Not Matched";
if (string.IsNullOrEmpty(range) || comp < 0)
{
result = "";
}
else
{
// removes characters from the string
// (http://stackoverflow.com/questions/4366422/how-to-remove-characters-from-a-string-using-linq)
string strDigits = new String(range.Where(Char.IsDigit).ToArray());
// creates upper and lower limits
int lower = (strDigits.Length > 0 ? (int)strDigits.Min() - 48 : 0);
int upper = (strDigits.Length > 1 ? (int)strDigits.Max() - 48 : lower);
// compares values
if (comp >= lower && comp <= upper)
{
result = "Matched";
}
else if (comp >= (lower - 1) && comp <= (upper + 1))
{
result = "Variation";
}
}
return result;
}
Marked as answer by Angie xu - MSFT on Dec 09, 2012 11:05 PM
happysack
Member
18 Points
46 Posts
(small amendment)Amending this code to find max and lower limits
Dec 03, 2012 05:16 PM|LINK
Now i have this piece of code already from this forum, works perfectly though my needs have slightly changed 'since testing!'
The code finds 2 numbers from the string.
then tests my variable against these numbers, if below/above by more than 1 gives no matched.
Else if varied by 1 gives Variation and if between the numbers gives Matched.
Here is the code.
public static string TestMatch(string range, int comp) { string result = "Not Matched"; if (string.IsNullOrEmpty(range) || comp < 0) { result = ""; } else { // removes characters from the string // (http://stackoverflow.com/questions/4366422/how-to-remove-characters-from-a-string-using-linq) string strDigits = new String(range.Where(Char.IsDigit).ToArray()); // creates upper and lower limits int lower = (strDigits.Length > 0 ? Convert.ToInt16(strDigits.Substring(0,1)) : 0); int upper = (strDigits.Length > 1 ? Convert.ToInt16(strDigits.Substring(1,1)) : lower); // compares values if (comp >= lower && comp <= upper) { result = "Matched"; } else if (comp >= (lower - 1) && comp <= (upper + 1)) { result = "Variation"; } } return result; }My change is that the variable string, which above is 'range' could now contain 3 or more numbers.
The good thing is the reamining code already wil match if between the 2, so rather than pulling the first 2 digits it can find for 'lower' and 'upper' I now need 'lower' and 'upper' to be the biggest/smallest number found in the string respectively. If only single number found then will act the same as it does.
I am sure this is easy and i thought i understood the code, however making this amendment is beyond me!
would like to say many many thanks in advance
rrrsr7205
Participant
1308 Points
316 Posts
Re: (small amendment)Amending this code to find max and lower limits
Dec 03, 2012 07:07 PM|LINK
@{ var input = "281379465"; var strDigits = new String(input.Where(Char.IsDigit).ToArray()); var max = (int)strDigits.Max(); var min = (int)strDigits.Min(); foreach(var d in strDigits) { @d @((int)d>min && (int)d<max?" inRange":(int)d==min?" is the minimum":" is the maximum") <br /> } }happysack
Member
18 Points
46 Posts
Re: (small amendment)Amending this code to find max and lower limits
Dec 04, 2012 06:15 AM|LINK
find the highest/lowest single digit in a string?
For instance my wont have spaces between the numbers?
If this is true could i amend my code like the following?
public static string TestMatch(string range, int comp) { string result = "Not Matched"; if (string.IsNullOrEmpty(range) || comp < 0) { result = ""; } else { // removes characters from the string // (http://stackoverflow.com/questions/4366422/how-to-remove-characters-from-a-string-using-linq) string strDigits = new String(range.Where(Char.IsDigit).ToArray()); // creates upper and lower limits int lower = (strDigits.max()); int upper = (strDigits.min()); // compares values if (comp >= lower && comp <= upper) { result = "Matched"; } else if (comp >= (lower - 1) && comp <= (upper + 1)) { result = "Variation"; } } return result; }GmGregori
Contributor
5564 Points
749 Posts
Re: (small amendment)Amending this code to find max and lower limits
Dec 06, 2012 05:00 PM|LINK
Try amending your function as follows:
public static string TestMatch(string range, int comp) { string result = "Not Matched"; if (string.IsNullOrEmpty(range) || comp < 0) { result = ""; } else { // removes characters from the string // (http://stackoverflow.com/questions/4366422/how-to-remove-characters-from-a-string-using-linq) string strDigits = new String(range.Where(Char.IsDigit).ToArray()); // creates upper and lower limits int lower = (strDigits.Length > 0 ? (int)strDigits.Min() - 48 : 0); int upper = (strDigits.Length > 1 ? (int)strDigits.Max() - 48 : lower); // compares values if (comp >= lower && comp <= upper) { result = "Matched"; } else if (comp >= (lower - 1) && comp <= (upper + 1)) { result = "Variation"; } } return result; }