Click here to Skip to main content
15,918,808 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
 

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