English 中文(简体)
删除最后一行的CRLF
原标题:remove CRLF in the last line

如何去除最后的<代码>crlf 准则如下:

  Dim fh As StreamReader
  Dim temp as string
  Dim temp1 as string
  fh = new StreamReader("haggis.txt")
  Dim s As String = fh.ReadLine()
  While not s Is Nothing

    temp = temp & s & Vbcrlf

  Console.WriteLine(temp)

  End While
  fh.Close()

haggis.txt

AAAA   
BBBB


 Return 
AAAA
BBBB
crlf  <---- I want to remove this.
最佳回答

您不应直接使用<条码>。 我认识到上述守则也只是一个例子,但你从未在座右边更新s,因此如果进入,那将是无限的休息。

Assuming you fix that, you want to use a StringBuilder for the String concatenation:

Dim temp As New System.Text.StringBuilder()

While s IsNot Nothing

    temp.Append(s).Append(vbCrLf)

End While

Dim answer As String = ""

  avoid trying to get a substring if it s blank
If temp.Length <> 0 Then

    answer = temp.ToString(0, temp.Length - vbCrLf.Length)

End If

To get it without the last vbCrLf, simply chop it off as I do above.

问题回答

Try this instead: use File.ReadAllLines() so that you don t have to use a vbCrLf at all. It loads all lines into a string array.

 Dim readText() As String = File.ReadAllLines("haggis.txt")
 Dim s As String
 For Each s In readText
     Console.WriteLine(s)
 Next

This could be an easy solution

    For Each 
        Do
            If (String.IsNullOrEmpty(strline)) Then
                strline = strline & objSR.ReadLine()
            Else
                strline = vbCrLf & objSR.ReadLine()
            End If
            objwriteline.strline()
        Loop
    Next




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签