String.Together Snazzy String Routines (Continued)
Imagine a scenario where you need to format a number into a currency amount and a percentage amount. Assume you have a form with a Textbox named Textbox1, and a couple of Label controls named Label1 and Label2. This code formats a number from Textbox1 and places the formatted results in the Label controls:
Dim s As String
Dim d As Decimal = CDec(TextBox1.Text)
s = String.Format("{0:C}", d) ' Currency
Label1.Text = s
s = String.Format("{0:P}", d) ' Percentage
Label2.Text = s
Using String.Format is largely a matter of finding the code that specifies what kind of formatting you want or need. This example shows the "C" code for currency and the "P" code for percentage, and there are codes for other formats such as scientific and hexadecimal. (Consult the VB .NET help files for a complete list.)
String.Format does more than let you insert characters. It also handles other chores such as rounding. For example, the currency formatter displays the currency amount formatted in dollars. It does so because I live in the United States, and my operating system setting is for currency in dollars. If you run the exact same code in another locale, the formatted output changes automatically to reflect the currency used in that locale. Number separators, such as the comma in the percentage amount, also change based on locale.
VB .NET includes more codes for formatting dates than for formatting numbers. You can choose long or sortable dates, and various orders of month, day, and year. For example, try altering the previous example to format a date a couple of different ways:
Dim s As String
Dim d As Date = CDate(TextBox1.Text)
s = String.Format("{0:D}", d) ' Long date
Label1.Text = s
s = String.Format("{0:s}", d) ' Sortable date
Label2.Text = s
This code works like the previous snippet, with one exception: It converts the contents of the textbox to a date, the codes in String.Format to a "D" for long date, and to an "s" for sortable date.
Note that some codes are case sensitive, so the lowercase "s" used in the date formatting code performs a different action than an uppercase "S." Also, the action associated with a code can vary based on what data type you want to format. "D" as used in this snippet formats a date as a long date, but "D" used with a decimal would format it as a standard decimal, with an optional number of leading zeros.
Compare Old and New VB Strings
Suppose you have two values entered by a user, and you need to make sure that they are in alphabetical order. In Classic VB, you would use the StrComp function, which returns a value of -1, 0, or 1, depending on the relationship of the two strings:
Dim FirstString As String = _
TextBox1.Text
Dim SecondString As String = _
TextBox2.Text
Dim i As Integer
i = StrComp(FirstString, SecondString)
Select Case i
Case -1 ' FirstString is lower
Case 0 ' strings are equal
Case 1 ' SecondString is lower
End Select
You can still use the StrComp function in .NET versions of Visual Basic. However, I recommend the Compare method of the String class instead. String.Compare works exactly the same way as StrComp in a simple example. You need to change only the line that contains StrComp:
i = String.Compare(FirstString, SecondString)
However, String.Compare offers more flexibility. It has a number of overloads, including a useful one that ignores case:
i = String.Compare(FirstString, SecondString, True)
This overload performs a case-insensitive comparison, but it returns the same values as the other overload.
Back to top
|