Use StringBuilder in ASP. NET

Source: Internet
Author: User

Good habits are very important for the growth of people, and good programming habits are also very important for the improvement of our programming capabilities. We should have a good style in programming, the logic of source code is concise and clear, and easy to read and understand is an important standard for good programs. Making our programs more approachable and more vivid is of great benefit to our programming. It can also help us get twice the result with half the effort. The unwritten standards observed during programming are beneficial to our communication with others and our re-learning. After all, sometimes the program we write is not just for ourselves, nor is it on the rise, No matter later. Good coding makes the source code readable and meaningful, consistent with other language conventions, and as intuitive as possible.

1. Handle other errors)

The most basic requirement for program robustness is the processing and capturing of program errors. in ASP. NET, the error processing mechanism is the same as that in other programming languages. You can use Try... Catch... Finally and so on, which is greatly improved compared with ASP. In addition, using these error handling methods can greatly improve the readability and debugging speed of the program. When combining these advantages, we should pay more attention to this.

2. string processing

In web design, string processing is almost the most common. Use ASP.. NET, the string processing speed is faster than ASP. NET, specifically added a string processing class StringBulider, using this class can complete some common string operations, and most importantly, using StringBuilder can greatly improve the string processing speed.

In ASP. NET, the most common is to use "&" to connect two strings:

 
 
  1. Dim myOutputString As String = "My name is" 
  2. Dim myInputString As String = " Alex" 
  3. myOutputStringmyOutputString = myOutputString & myInputString  
  4. Response.Write(myoutputString) 


Now let's take a look at the use of StringBuilder. When using StringBuilder, we can perform some basic operations on strings, such as Append, Replace, Insert, and Remove, now let's take a look at the specific example.

1) Use Append in StringBuilder
Append is the same as Append in other languages, that is, adding other characters at the end of the string.

 
 
  1. Dim sb as StringBuilder = New StringBuilder()  
  2. sb.append( "<table border=\\\'1\\\' width=\\\'80%\\\'>" )  
  3. For i = 0 To RowCount - 1  
  4. sb.Append("<tr>")  
  5. For k = 0 To ColCount - 1  
  6. sb.Append("<td>")  
  7. sb.Append( dt.Rows(i).Item(k, DataRowVersion.Current).toString())  
  8. sb.Append( "</td>" )  
  9. Next  
  10. sb.Append("<tr>")  
  11. Next  
  12. sb.Append( "</table>")  
  13. Dim strOutput as String = sb.ToString()  
  14. lblCompany.Text = strOutput 


In the above program, the Append method is used to output a table. Note that the StringBulider must first use ToString () the method can be converted to the String type for direct output. In the above example, all we see is a direct string of Append. In fact, this method has a very convenient function, that is, it can directly Append other types of variables, for example, you can Appemd a value of the Integer type directly. Of course, the output will be automatically converted into a string:

 
 
  1. Sub Page_Load (Source As Object, E As EventArgs)
  2. Dim sb As System. Text. StringBuilder
  3. Dim varother As Integer
  4. Varother=9999 
  5. Sb=NewSystem. Text. StringBuilder ()
  6. Sb. append ("<Font Color=\\ 'Blue \\\'>You can Append other types:</Font>")
  7. Sb. append (varother)
  8. Response. write (sb. toString ())
  9. End Sub


2) use of other methods in strings

We can also use other methods to look at the common ones:
Insert method. you can Insert other characters at the specified position. Use this method: Insert position, Insert character );
The Remove method can be used to delete a specified number of characters at a specified position. Usage: Remove the actual position, number of characters );
The Replace method can replace a specified character. Usage: Replace is replaced by a string)

3. Close the Connection and DataReader of the database.

When programming with ASP, we know that after connecting to a database, we must close the connection and set it to NoThing. In Asp. NET, we still need to use it like this, however, in ASP. NET, due to the use of ADO. NET. Therefore, there are some subtle differences in some related processing. These differences are often the most important during our design. Now, let's take an example to see what issues need to be paid attention to in common ADO. NET operations.

  1. Introduction to ASP. NET Framework
  2. Introduction to ASP. NET Applications
  3. Processing Methods of ASP. NET framework
  4. Analysis on ASP. NET Security Architecture
  5. Overview ASP. net mvc and FubuMVC core framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.