Fun With Strings


String Operator(s)

Recall that a string consists of zero or more characters enclosed with double quotation marks, such as the string "Fred". When working with strings, it is frequently useful to be able to combine (i.e. bind together) two or more strings. The single string operator which performs this is the (binary) concatenation operator, implemented using the ampersand (&) character. If we wanted to combine the string "Fred" with the string "Flintstone", we could use the concatenation operator as follows: "Fred" & "Flintstone". This would produce the single string "FredFlintstone" (note no spaces between Fred and Flintstone). Or similarly, we could assign string values to variables as follows:
	Dim strFName As String = "Fred"
	Dim strLName As String = "Flintstone"

	Console.WriteLine (strLName & ", " & strFName)
which would produce the single string :
	Flintstone, Fred
in the console window. Notice here there are three strings being combined; the string "Flintstone", the string ", " and the string "Fred", however each concatenation operator is still working on two operands since it is a binary operator.

String Formatting

When displaying output, we often wish to provide some specific format for this output. It is important to note that formatting String data does not change the value of the data, but merely formats it appearance. There are numerous ways to format strings, some of the more popular formatting methods include:

FormatNumber

The FormatNumber method is used to format an expression as a general number in some particular way, such as the number of places before or after the decimal point. The value returned from the FormatNumber method is a String type. General syntax (in part) looks like:

	FormatNumber (expression [,NumDigitsAfterDecimal][,IncludeLeadingDigit])
where:
An example of this method is:
	decAnswer = 123.45D
		
	txtAnswer.Text = FormatNumber(decAnswer, 3)		'results in (String) 123.450 

FormatCurrency

The FormatCurrency method is used to format an expression as a currency value in some particular way, with a leading currency symbol (e.g. a $ in the U.S.) and perhaps the number of places before or after the decimal point. The value returned from the FormatCurrency method is a String type. General syntax (in part) looks like:

	FormatCurrency (expression [,NumDigitsAfterDecimal][,IncludeLeadingDigit])
where, as above: An example of this method is:
	decAnswer = 123.45D
		
	txtAnswer.Text = FormatCurrency(decAnswer)		'results in (String) $123.45 
or looking at another example
	decAnswer = 0.45D
		
	txtAnswer.Text = FormatCurrency(decAnswer,,TriState.False)	'results in (String) $.45 

ToString

There are often times we would like to convert a non-string data type to a String type for output formatting. For example, we might want to concatenate a String type with a Decimal value, or assign a Decimal value to a TextBox. One way to do this is using the .ToString method. Most, if not all primitive data types support the .ToString method. An example of this is as follows:

	txtOutput.Text = decOutput.ToString
Hopefully, it is obvious that this method returns a String data type.

Additional ToString Examples

Below is some VB code to further exemplify formatting examples using the .ToString method. These use the Console.WriteLine method for simplicity, however these will also work when formatting control objects such as textboxes.

	Dim myDecNumber As Decimal = 12345.678D

        Console.WriteLine(vbCrLf & "No formatting: " & myDecNumber.ToString & vbCrLf)

        'Demonstrate the standard formats
        Console.WriteLine("Standard Formats:" + vbCrLf)
        Console.WriteLine("     Currency formatting: " & myDecNumber.ToString("C"))
        Console.WriteLine("     Exponential formatting: " & myDecNumber.ToString("E"))
        Console.WriteLine("     Fixed-point formatting: " & myDecNumber.ToString("F2"))
        Console.WriteLine("     General formatting: " & myDecNumber.ToString("G"))
        Console.WriteLine("     Number formatting to 2 decimal places: " & myDecNumber.ToString("N2"))
        Console.WriteLine("     Number formatting to 3 decimal places: " & myDecNumber.ToString("N3"))
        Console.WriteLine("     Number formatting to 4 decimal places: " & myDecNumber.ToString("N4"))
        Console.WriteLine("     Percent formatting: " & myDecNumber.ToString("P0"))
The output of from the above code appears as follows:
No formatting: 12345.678

Standard Formats:

     Currency formatting: $12,345.68
     Exponential formatting: 1.234568E+004
     Fixed-point formatting: 12345.68
     General formatting: 12345.678
     Number formatting to 2 decimal places: 12,345.68
     Number formatting to 3 decimal places: 12,345.678
     Number formatting to 4 decimal places: 12,345.6780
     Percent formatting: 1,234,568 %
See additional information from Microsoft on standard numeric formatting
here.

1 The TriState type is a ternary type (i.e. three values) whose values consist of TriState.True, TriState.False, and Tristate.UseDefault. TriState.True yields a True value, TriState.False yields a False value, and TriState.UseDefault uses the computers regional settings.


String Manipulation



Next Section: Procedures Table of Contents


©2007, Mark A. Thomas. All Rights Reserved.