回车打印到文本VB.net

问题描述

我正在尝试打印到文本文件,但是我试图将我的两个if语句输出打印在同一行上。目前,每个人都在自己的行上打印,我想知道如何让它们保持在同一行上。我尝试执行Replace(vbCrLf,“”),但是它不起作用,或者我没有将其放置在正确的位置。

If (atr.Value = "Cat") Then
    file.WriteLine(String.Format("{0} - {1} ",attribute.Value,atrue.Value))
End If
If (atr.Value = "Money") Then
    file.WriteLine(String.Format("{0} - {1} ",atrue.Value))
End If

输出

Cat - 19
Money - H2E

所需的输出

Cat - 19 Money - H2E

解决方法

这里有些混乱

If (atr.Value = "Cat") Then
    file.WriteLine(String.Format("{0} - {1} ",attribute.Value,atrue.Value))
End If
If (atr.Value = "Money") Then
    file.WriteLine(String.Format("{0} - {1} ",atrue.Value))
End If

您有3个变量:

  • atr
  • atrue
  • attribute

我希望其中之一是多余的/重复的或错字的。

我还猜想这是在一个循环中运行的,变量值在每个循环中都在变化,并且您只想在编写“ Money”之后切换到下一行。

(Some loop)

    'always write the value
    file.Write($"{attribute.Value} - {atrue.Value} "))

    If (atr.Value = "Money") Then file.WriteLine() 'only write a newline if we wrote Money