How to expand a string to display a fixed width in VB. NET

Source: Internet
Author: User

In VB. NET, when you need to display data on the console or prepare to print data, you may need to adjust the column width to display data of a fixed length. This article describes how to use the padleft method of the string object and the padright method to expand the string for fixed width display.

PadleftAnd padrightMethod

Padleft and padright are two methods of the string class. They can be used to fill spaces on the left and right sides of the string respectively. The two methods accept an integer that represents the total length, and the number of spaces added is equal to the total length of the fill minus the current length of the string.

Note: When formatting a string to a fixed width display, you should use a fixed width font, such as courier, because the fixed width characters share the same width. Otherwise, the filling will be invalid.

Another alternative that programmers often use is to use TAB characters to obtain an approximate fixed-width display format. One problem with using tabs is that when a certain length is longer than the display length, it will expand the tab, which will cause the lines to be not well aligned.

In list A, we show you how to use the padleft and padright methods to display strings. (To run this example, we added a drop-down list listbox1 and set its font to a fixed-width font.) Figure 1 shows the result of running the code in list.

In this example, we define an integer variable I and two string Arrays: strarrseasons and strarrweather. Assign an initial value to each array as a predefined value. Then, we pass each array to the padarray function and specify the expansion on the left side of the string.

Code:
  1. Private sub padstrings ()
  2. Dim I as integer = 0
  3. Dim strarrseasons () asstring = {"Winter", "Spring", "Summer", "fall "}
  4. Dim strarrweather () asstring = {"cold", "warm", "hot", "cool "}
  5. Padarray (strarrseasons, true)
  6. Padarray (strarrweather, false)
  7. Dim strall as string
  8. For I = 0 to strarrseasons. Length-1
  9. Listbox1.items. Add (strarrseasons (I) & "& strarrweather (I ))
  10. Next
  11. End sub
  12. Private sub padarray (byref strarray () as string, byval bpadleft as Boolean)
  13. Dim I as integer = 0
  14. Dim imaxlength as integer = 0
  15. Dim stritem as string
  16. For each stritem in strarray
  17. If stritem. length> imaxlength then imaxlength = stritem. Length
  18. Next
  19. For I = 0 to strarray. Length-1
  20. If bpadleft = truettings
  21. Strarray (I) = strarray (I). padleft (imaxlength)
  22. Else
  23. Strarray (I) = strarray (I). padright (imaxlength)
  24. End if
  25. Next
  26. End sub

 

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.