vb.net - 什么是仅使用制表符(不使用制表位)从文本框中的字符串数组显示对齐表格的最简单方法
问题描述
我有一个包含 5 个条目的字符串数组,其中每个字符串由 7 个用逗号分隔的字段组成。这些字段的长度不同。我使用的是等宽字体,所以我可以用标签对齐东西。
我已经建立了一个遍历字符串数组的循环,拆分数组中的字符串,以确定每个“列”中的哪个字符串最长。然后是另一个循环,我在其中组装一个带有字段的字符串,后跟一个 vbTab,如果当前字段比最大长度短 8 的倍数,我添加额外的 vbTabs(所以如果它短 8,则添加一个额外的 vbTab,16 = 2 vbTabs 等)。
但在某些情况下,我很难让事情正确对齐。
代码:
Sub WriteTidyBlockH_layoutissues(list As List(Of String))
'Takes a list of strings as argument
'Writes it out as a table
'Assumes first line is a header (will be bolded)
'Go through list, split table fields and determine their lengths
Dim strLineFields As String() 'string array to hold the fields in a single line
Dim strLineField As String 'an individual field in a line
Dim FieldMaxLengths As New List(Of Integer)() 'list that will hold max length of each column
Dim i, j, k, intLines, intColumns As Integer
Dim strLine As String 'string to build a tabbed line in
Dim Tabs As Byte
'determine number of lines
intLines = list.Count - 1
'determine number of columns by counting the commas
intColumns = list(0).Count(Function(c As Char) c = ",")
For i = 0 To intColumns
FieldMaxLengths.Add(0)
Next
Dim strFields(intLines, intColumns) As String '2 dimensional array of strings, containing ALL fields
i = 0
For Each strListElement As String In list
j = 0
strLineFields = Split(strListElement, ",")
For Each strLineField In strLineFields
strFields(i, j) = strLineFields(j)
If strLineFields(j).Length > FieldMaxLengths(j) Then
FieldMaxLengths(j) = strLineFields(j).Length
End If
j += 1
Next
i += 1
Next
For i = 0 To intLines
strLine = ""
For j = 0 To intColumns
If Not j = intColumns Then
strLine += strFields(i, j) + vbTab 'one tab is always needed for every field, except the last
If strFields(i, j).Length <= FieldMaxLengths(j) Then
'strLine += strFields(i, j) + vbTab
'figure out how many additional tabs necessary
Tabs = (FieldMaxLengths(j) - strFields(i, j).Length) \ 8 'Div
For k = 1 To Tabs
strLine += vbTab
Next
End If
Else
'last column, don't add tabs
strLine += strFields(i, j)
End If
Next
If i = 0 Then
WriteOut(strLine, 0, True)
Else
WriteOut(strLine)
End If
Next
End Sub
输出当前如下所示:
Bank Location Capacity Speed Manufacturer Part Number Serial Number
BANK 0 ChannelA-DIMM0 8GB 1600 Kingston 99U5471-066.A00LF 24E8D583
BANK 1 ChannelA-DIMM1 8GB 1600 Kingston 99U5471-054.A00LF 30269BB7
BANK 2 ChannelB-DIMM0 8GB 1600 Kingston 99U5471-058.A00LF 182C9113
BANK 3 ChannelB-DIMM1 8GB 1600 Kingston 99U5471-054.A00LF D63F4C11
注意我在格式化时遇到了一些麻烦。在实际输出中,只有Capacity 和Serial Number 未对齐(太左),而Spped、Manufacturer 和Part Number 与它们下方的数据正确对齐。
注意:WriteOut 只是一个将单个字符串写入文本框并附加 vbCrLf 的子程序。其他两个参数是可选的(颜色和粗体)。
解决方案
推荐阅读
- c# - 无法接收 UDP 数据报,发送到 IPv6“所有节点”多播组
- python - 标记为已删除(Django)(在所有表中标记)或(在一个中)
- mysql - 如何在每次服务启动时运行 MySQL 查询?
- blueprism - Blueprism 无法匹配两个相同的数据项
- compression - 这个想法可以无损压缩真正的随机数据吗?
- reactjs - Redux Firestore onSnapshot
- nuxt.js - NuxtJs 2.0 - 如何避免捆绑客户端不需要的库
- html - Bootstrap Carousel 图像位置中心
- angular - Angular 6 - ngx-mqtt 6.7.0 - 连接建立错误:net::ERR_CONNECTION_REFUSED
- apache-kafka - 如何在 Kafka 或 KSQL 中修改或添加主题的键