Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 调试文本到文件格式_Vb.net_Math_Rounding - Fatal编程技术网

Vb.net 调试文本到文件格式

Vb.net 调试文本到文件格式,vb.net,math,rounding,Vb.net,Math,Rounding,嘿,我希望简化以下代码,以便输出正确数量的=,以匹配输出部分的顶部/底部 例如: ======================================================================== =======================This would be the text here====================== ===============================================================

嘿,我希望简化以下代码,以便输出正确数量的=,以匹配输出部分的顶部/底部

例如:

========================================================================
=======================This would be the text here======================
========================================================================
Dim strDebug string = "Bob The Builder"
cnt = 72 - strDebug          '72-15 = 57
cnt = Math.Round(cnt / 2, 2) '57/2 = 29 (28.5 rounded)
文本,这将是这里的文本,将被发送到函数。这可以是从4个字符到最多72个字符的任何内容。我想看看是否有一种更简单的编码方法,然后我使用以下方法:

Dim cnt As Integer = 0
Dim ch As Char = ""

For Each c As Char In _tmpDebugArray(0)
    If c = ch Then cnt += 1
Next

cnt = Math.Round((cnt - 72) / 2, 2)
cnt将为我提供调试信息名称左侧和右侧需要使用的=数量,以匹配输出部分的=的顶部/底部

例如:

========================================================================
=======================This would be the text here======================
========================================================================
Dim strDebug string = "Bob The Builder"
cnt = 72 - strDebug          '72-15 = 57
cnt = Math.Round(cnt / 2, 2) '57/2 = 29 (28.5 rounded)

因此,在上面的示例中,=将在左侧显示28,然后调试字符串将在生成器上显示29=,然后在其右侧显示29。尽管根据调试字符串的长度,这里和那里的值往往会相差1。

使用固定的宽度并在左右两侧填充文本可能更容易

Sub DisplayText(ByVal text As String)

    Const WIDTH As Integer = 72
    Const DISPLAY_CHAR As String = "="c

    Console.WriteLine("".PadLeft(WIDTH, DISPLAY_CHAR))
    Console.WriteLine(text.PadLeft((WIDTH + text.Length) / 2, DISPLAY_CHAR).PadRight(WIDTH, DISPLAY_CHAR))
    Console.WriteLine("".PadLeft(WIDTH, DISPLAY_CHAR))

End Sub

假设您仅以固定宽度字体查看这些字符串,则可以使用
PadLeft
PadRight
将字符串填充到正确的长度。下面的函数可以对任何字符串、填充字符和长度执行此操作

Function PadStringCentre(str As String, ch As Char, len As Integer) As String
    Dim numLeft As Integer = (len - str.Length) \ 2 + str.Length 
    Dim numRight As Integer = len - str.Length - numRight 
    Return str.PadLeft(numLeft,ch).PadRight(len, ch)
End Function
你可以这样称呼它

Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Label1.Text = PadStringCentre("Bob The Builder", "="c, 72)
End Sub

工作得很好!谢谢,荷花。