Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have the following functions which detects and converts inches and feet in a string.

VB
'inches
   Private Shared Function Convert(value As String) As String
       Return Regex.Replace(value, "([\d.]+)'", Function(m) Format(Single.Parse(m.Groups(1).Value) * 0.3048, "Standard"))
   End Function

   'feet
   Private Shared Function ConvertFeet(value As String) As String
       Return Regex.Replace(value, "([\d.]+)''", Function(m) Format(Single.Parse(m.Groups(1).Value) * 0.0254, "Standard"))
   End Function


Please I want to combine these functions into one so than
eg. given 2'5'' I will have 0.74 meters
Posted
Comments
Sergey Alexandrovich Kryukov 27-Oct-14 7:25am    
If you want to combine it, combine it. What's the problem?
—SA

oh, I see you wish to supply something like 5'6" (5 feet 6 inches).. well, in c# I'd start with something like

XML
String feetinchRegexString = @"
    (?<feet>[\d.]+) # Feet
    (\')        # ' Feet Marker
    (?<inch>[\d.]+) # Inch
    (\")        # " Inch Marker
";

# NB Have to use RegexOption.IgnorePatternWhiteSpace


I doubt that regex is actually usable, Im just surmising some of the things you'd need to think about - it doesn't take into account things like

5' 3/16"

for example - NB in C# using 'named groups' you would extract 'feet' and 'inches' like this

Regex feetinchesRegex = new Regex(feetinchesRegexString, RegexOption.IgnorePatternWhiteSpace);
Match match = regex.Match(sample);
    if (match.Success)
    {
        Console.WriteLine(match.Groups["Feet"].Value);
    Console.WriteLine(match.Groups["Inches"].Value);
    }



Im pretty sure you'd be able to do the same in VB.Net, and re-do my regex for all conditions - then you simply combine feet and inches into a 'double' perhaps and apply the correct multiplication factor
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Oct-14 8:04am    
I wouldn't bother checking it up, but it looks very reasonable, especially taking into account 3/16 kind of things. 5ed.
—SA
Garth J Lancaster 27-Oct-14 8:08am    
thanks SAK - yes, this is probably wasted effort ;-)
Sergey Alexandrovich Kryukov 27-Oct-14 8:25am    
No, no, I didn't mean your effort was wasted; and I don't think so.
I tried to say that I did not bother checking up correctness of your solution.
—SA
wizy@2020 27-Oct-14 9:41am    
Thanks for all your effort, but if there is any other way to achieve this I will gladly appreciate it.
Thanks7872 28-Oct-14 1:46am    
Is there any specific reason you have problem with all the solution you have been provided and asking for something different?
There is no way to combine those functions into one and have it work reliably.

A better way to do it would be to parse the string into a structure that represents a length. You can then call a overloaded ToString method on that structure that can give you a string in any format you want, English or Metric. You, of course, would have to write that method yourself, dictating what parameters you'll accept.
 
Share this answer
 
Why are there regular expressions involved in a conversion from feet to meters? First of all create a normal conversion function (sorry I feel dirty when I use VB so here's some C#):

C#
public static double ConvertFeetToMeters(double feet, double inches)
{
      return (feet + (inches / Convert.ToDouble(12))) * 0.3048;
}


If you need to accept the extra quotations on the input (e.g. 5'2") then parse it off in a separate function and then pass it to the normal and readable function above.

UPDATE:

Here is a function that would call the above function. You should keep them as separate functions as best practice though. If you are accepting input from a user, then you should just use two textboxes, one for feet and one for inches and forgo this nonsense. If you are reading the values from a database, then ya'll have more problems then I could ever explain. I'm dying to know why the feet and inches are coming into the function with the unnecessary punctuation attached. Good luck.

C#
// value MUST BE provided in format 5'2", 6'0", etc. with the unnecessary punctuation
        public static string ParseFeetToMeters(string value){
            string[] values = value.Split ('\'');
            double feet = Convert.ToDouble(values[0].Trim());
            double inches = Convert.ToDouble(values[1].Remove(values[1].Length-1).Trim());
            return ConvertFeetToMeters(feet, inches).ToString();
        }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 27-Oct-14 8:26am    
Probably you just missed the point: the problem is not conversion from two double value to another double value. The problem as parsing the string showing length in feet and inches.
—SA
Robert Welliever 27-Oct-14 8:55am    
The problem is conversion from feet and inches to meters with using a string input and output. The function I wrote above is the correctly written portion that does the conversion from feet and inches to meters. The conversion function should not be combined with input parsing and string manipulation, that's just rookie. I'll be honest, I've read a number of your comments now and they're generally just pretentious and ignorant.
Sergey Alexandrovich Kryukov 27-Oct-14 9:58am    
Yes, but this wasn't a problem.

And here is my suggestion: please, let's stop any personal characteristics. This is not acceptable in this forum. I never allowed myself to try to characterize you personally in any way, only your particular comments and opinions. You have no reasons for such characteristics, and this is ethically not acceptable.

—SA
Robert Welliever 27-Oct-14 22:56pm    
Calling your comments pretentious and ignorant is perfectly ethical and entirely accurate. Don't tell me what I can and can't write and quit trolling me.
Sergey Alexandrovich Kryukov 27-Oct-14 23:05pm    
Nobody is trolling you. We either give up any personal characteristics or stop talking.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900