Get the App
SLTechnology News&Howtos  ›  Development  › 

What's the use of VB.NET StringWriter?

Shulou Source: shulou.com Published: 2022-06-02 08:52:17 10月27日 Update

Editor to share with you what is the use of VB.NET StringWriter, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The emergence of VB.NET programming language has brought great help for developers, through the use of various classes, can help us to complete a lot of functional requirements. VB.NET StringWriter is located under the System.IO namespace. StringWriter can also replace the String class to complete string concatenation operations, which is more efficient.

A simple example of use is as follows:

Dim swA As New System.IO.

StringWriter

Dim arrStr () As String =

{"Northsnow", "Snow in the North"}

SwA.WriteLine (arrStr (0))

SwA.WriteLine (arrStr (1))

SwA.WriteLine (String.Format

("{0}-{1}-{2}", arrStr (2)

ArrStr (3), arrStr (4))

Me.TextBox1.AppendText

(Environment.NewLine)

Me.TextBox1.AppendText

(swA.ToString)

Me.TextBox1.AppendText

(Environment.NewLine)

The output of this program is:

Snow in the north of Northsnow

You must have noticed that the WriteLine method is used in this example, but the swA.WriteLine ("Northsnow") method is equivalent to

SwA.Write ("Northsnow") swA.Write (swA.NewLine)

You can also write information to StringBuilder. A complete example is as follows:

Dim sbA As New System.Text.StringBuilder

SbA.AppendLine ("this is a StringWriter

Example of writing to StringBuilder: ")

'The data of this StringWriter is written to the specified StringBuilder

Dim swA As New System.IO.StringWriter (sbA)

Dim arrStr () As String = {"Northsnow"

"Snow in Saibei"}

SwA.WriteLine (arrStr (0))

SwA.WriteLine (arrStr (1))

SwA.WriteLine (String.Format ("{0}-{1}-{2}")

ArrStr (2), arrStr (3), arrStr (4))

Me.TextBox1.AppendText (Environment.NewLine)

Me.TextBox1.AppendText (swA.ToString)

Me.TextBox1.AppendText (Environment.NewLine)

'you can use the GetStringBuilder of StringWriter

Method returns a StringBuilder

Dim sbB As System.Text.StringBuilder =

SwA.GetStringBuilder ()

SbB.AppendLine ("in the returned StringBuilder

Add a row in ")

Me.TextBox1.AppendText (Environment.NewLine)

Me.TextBox1.AppendText (sbB.ToString)

SwA.Close () 'attention, here will be VB.NET StringWriter

Closed, but you can still use StringBuilder later

SbA.AppendLine ("in StringBuilder

Add a new line in ")

Me.TextBox1.AppendText (sbA.ToString)

The result returned by this example is:

This is an example of writing VB.NET StringWriter to StringBuilder:

Snow in the north of Northsnow

A careful friend may have found out. SbA and sbB are the same StringBuilder. That is, sbA and sbB influence each other. For one of the operations, the other also changes. What's going on?

In fact, they are a quote at this time. That is, the StringBuilder object returned through GetStringBuilder is actually a reference to the original StringBuilder object.

The above is all the content of this article "what's the use of VB.NET StringWriter?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Examples methods articles this is help one line that is content objects results careful not so two in use people information functions can be passed name answer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Information OPPO Reno macOS MySQL