The System.String class - Management of text

Visual Basic. NET has changed the way you work with text strings with respect to previous versions. Menejo All of these new features are in the System.String class. Now each text variable is an object of type String.

All classes in Visual Basic. NET has a method. ToString to display a text format content.

In visual basic.net have two ways of working with text or string variables:

- Using the old features we know from "always" as left, right, trim, replace, etc..

- Using new methods of the System.String class. NET

To use the chain of visual basi "traditional" (to call in some way to previous versions. NET) are available in the Microsoft.VisualBasic namespace for example:

 microsoft.VisualBasic.Left ("Text1", 5)

here are some methods and properties of the String object

Properties

Stringtabla1

Methods

Stringtabla2

and now let's see some examples in which use some of these properties and methods:

To find the longitus of a text we use the Length property:


 dim string1 as string = "some text"

 msgbox (texto1.Length) 'will display 19

 'We can do it directly on the text

 msgbox ("any other text." Length) 'will display 21

To align the contents of a text string or use PadLeft PadRight

These two methods are new and have no traditional equibalencia visual basic. They increase the chain by aligning its contents to the left or right.

To view the following working example just copy the code in the Load event of a form and run it.

Let me briefly explain what you do: first create an object and parameterized textbox to display multiple lines with the multiline property to True. Makes the textbox fills the form with the Dock property to Fill and as what we see is the alinación of the strings is very important to use a monospaced font such as Courier New. This is very important because if we use a source other is monospaced not see the texts properly aligned. Oh ... and the constant represents vbCrlf carriage return character combined with a line feed character for printing and viewing.
Finally, as textbox1 control I'm not creating in design mode (by dragging and dropping the control on the form) but think it through code, I can not see it unless you add it to the checklist form. To do this use the last line of code in the example.

    Dim textbox1 As New TextBox
 textbox1.Multiline = True
 textbox1.Dock = DockStyle.Fill
 textbox1.ScrollBars = ScrollBars.Vertical
 New System.Drawing.Font textbox1.Font = ("Courier New", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType (0, Byte))
 For i As Integer = 1 To 100
     i.ToString.PadLeft TextBox1.Text & = (4). PadRight (10, "-") & "is half of" & (i * 2). ToString.PadLeft (3) & vbCrLf
 Next
 Me.Controls.Add (textbox1)

Formatting numbers

Using the String class can format numbers, as shown in the following example:

 Dim number As Double
 number = 10580.65
 Dim result As String

 result = numero.ToString ("G") '10580.65
 numero.ToString result = ("N") '10580.65
 result = numero.ToString ("E") '+004 1058065E
 result = numero.ToString ("000,000.00") '010,580.65
 result = numero.ToString ("# # #, # # 0.00") '10580.65

 number = 0.63
 result = numero.ToString ("P") '63.00%
 result = numero.ToString ("# # 0.00%") '63.00%
 result = numero.ToString ("# # 0%") '63%

Comment