Is there a way to programmatically convert VB6 Formatting strings to .NET Formatting strings?

前端 未结 3 1714
谎友^
谎友^ 2020-11-30 12:13
  1. Does anyone know of a good reference for VB6 format-strings?
  2. Does anyone know of a converter from VB6 formatting strings to .NET strings?

I\'m

3条回答
  •  半阙折子戏
    2020-11-30 12:26

    Here's some F# code that translates the majority of pre-defined and custom VB6-style numeric and date format strings to something suitable for String.Format. It's easily called from C# or VB of course.

    open System
    
    module VB6Format =
    
        /// Converts a VB6-style format string to something suitable for String.Format()
        let Convert(vb6Format) =
            if String.IsNullOrWhiteSpace(vb6Format) then "{0}" else
            match if vb6Format.Length > 1 then vb6Format.ToUpperInvariant() else vb6Format with
            // PREDEFINED NUMERIC: http://msdn.microsoft.com/en-us/library/y006s0cz(v=vs.71).aspx
            | "GENERAL NUMBER" | "G"       -> "{0:G}"
            | "g"                          -> "{0:g}"
            | "CURRENCY" | "C" | "c"       -> "{0:C}"
            | "FIXED" | "F"                -> "{0:F}"
            | "f"                          -> "{0:f}"
            | "STANDARD" | "N" | "n"       -> "{0:N}"
            | "PERCENT" | "P" | "p"        -> "{0:P}"
            | "SCIENTIFIC"                 -> "{0:E2}"
            | "E" | "e"                    -> "{0:E6}"
            | "D" | "d"                    -> "{0:D}"
            | "X" | "x"                    -> "{0:X}"
            | "YES/NO" | "ON/OFF"          // we can't support these
            | "TRUE/FALSE"                 -> "{0}"
            // PREDEFINED DATE/TIME: http://msdn.microsoft.com/en-us/library/362btx8f(v=VS.71).aspx
            | "GENERAL DATE" | "G"         -> "{0:G}"
            | "LONG DATE" | "D"            -> "{0:D}"
            | "MEDIUM DATE"
            | "SHORT DATE" | "d"           -> "{0:d}"
            | "LONG TIME" | "T"            -> "{0:T}"
            | "MEDIUM TIME"
            | "SHORT TIME" | "t"           -> "{0:t}"
            | "M" | "m"                    -> "{0:M}"
            | "R" | "r"                    -> "{0:R}"
            | "s"                          -> "{0:s}"
            | "u"                          -> "{0:u}"
            | "U"                          -> "{0:U}"
            | "Y" | "y"                    -> "{0:Y}"
            // USER-DEFINED: http://msdn.microsoft.com/en-us/library/4fb56f4y(v=vs.71).aspx
            //               http://msdn.microsoft.com/en-us/library/73ctwf33(v=VS.71).aspx
            // The user-defined format strings translate more-or-less exactly, so we're just going to use them.
            | _                            -> sprintf "{0:%s}" vb6Format
    

提交回复
热议问题