Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 652
GSerg Avatar asked Feb 27 '23 18:02

GSerg


2 Answers

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 @@@@@"))
like image 110
Migration Specialist Avatar answered Mar 07 '23 01:03

Migration Specialist


This MSDN page seems to confirm that support was dropped from VB6 to VB.NET. You would have to implement it yourself, look around on the internet for some 3rd party code or (preferrably) rewrite the code to use String.Format and/or ToUpper/ToLower.

Your last example would be something like:

myString = String.Format("{0,-3}... not {1,-5}", "hi".ToUpper(), "there".ToUpper())

You could implement your own IFormatProvider to support uppercasing and lowercasing through format strings, but I'm not sure it's worth doing that.

like image 28
Thorarin Avatar answered Mar 07 '23 00:03

Thorarin