.netvb.netstring-formattingvb6-migration

VB6 Format function: analog in .NET


There is String.Format function that is referred to in the documentation as the analog for Format function from VB6. There's also Format function from VisualBasic namespace that is provided for compatibility and basically has same powers as String.Format.

Indeed, those two format dates and numbers.

But VB6's function was also able to format strings:

? format$("hi there", ">")
HI THERE
? format$("hI tHeRe", "<")
hi there
? format$("hi there", ">!@@@... not @@@@@")
HI ... not THERE

String.Format is not able to do that, as far as I'm concerned, nor is the new Format. I also couldn't find any mention in the compatibility Format documentation that certain parts of VB6 functionality is lost, seems like the feature was deprecated "silently."

Is there anything in the framework that can do this type of formatting?


Solution

  • Another solution to look at is to use the Microsoft.VisualBasic.Compatibility.VB6 namespace, which contains several classes and methods that are backwards compatible with Visual Basic 6. It's primarily meant for upgrade tools, but it will save you the hassle of having to purchase a migration tool or write the code yourself.

    MSDN Documentation: Support.Format Method (Microsoft.VisualBasic.Compatibility.VB6)

    The parameters don't change and it basically supports the same functionality at least given your examples:

    Imports Microsoft.VisualBasic.Compatibility.VB6
    
    Console.WriteLine("HI THERE ")
    Console.WriteLine(Support.Format("hi there", ">"))
    
    Console.WriteLine("hi there ")
    Console.WriteLine(Support.Format("hI tHeRe", "<"))
    
    Console.WriteLine("HI ... not THERE")
    Console.WriteLine(Support.Format("hi there", ">!@@@... not @@@@@"))