如何在字符串中将英尺转换为英寸 [英] How do I convert feet to inches in a string

查看:138
本文介绍了如何在字符串中将英尺转换为英寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能可以检测并转换字符串中的英寸和英尺。



I have the following functions which detects and converts inches and feet in a string.

'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





请将这些功能组合成一个,而不是

,例如。给定2'5''我将有0.74米



Please I want to combine these functions into one so than
eg. given 2'5'' I will have 0.74 meters

推荐答案

哦,我看到你希望提供类似5'6(5英尺6英寸)的东西..好吧,在c#中我会从类似



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

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

# NB Have to use RegexOption.IgnorePatternWhiteSpace





我怀疑正则表达式实际上是否可用,我只是猜测一些你需要考虑的事情 - 它没有考虑像



5'3 / 16



例如 - 在C#u中的NB唱'命名组',你会像这样提取'英尺'和'英寸'





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);
    }







我很确定你能做到在VB.Net中也一样,并且在所有条件下重新执行我的正则表达式 - 然后你只需将英尺和英寸组合成'双'并应用正确的乘法因子




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


没有将这些功能合二为一,使其可靠运行的方法。



更好的方法是将字符串解析为表示长度的结构。然后,您可以在该结构上调用重载的ToString方法,该方法可以为您提供所需格式的字符串,英语或公制。当然,您必须自己编写该方法,指定您接受的参数。
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.


为什么从英尺到米的转换涉及正则表达式?首先创建一个正常的转换函数(抱歉,当我使用VB时我觉得很脏,所以这里有一些C#):



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#):

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





如果您需要接受输入上的额外报价(例如5'2),请解析它在一个单独的函数中,然后将其传递给上面的正常和可读函数。



更新:



这是一个可以调用上述函数的函数。你应该把它们作为单独的函数作为最佳实践。如果你接受用户的输入,那么你应该只使用两个文本框,一个用于英尺,一个用于英寸并放弃如果你正在阅读数据库中的值,那么你会遇到更多问题,然后我就可以解释了。我很想知道脚和英寸为什么会附加不必要的标点符号来进入函数。祝你好运。





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.

// 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();
        }


这篇关于如何在字符串中将英尺转换为英寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆