简体   繁体   中英

Parsed multiline TextBox string contains unwanted line breaks characters

I am trying to parse an address out of a multi-line text box, like this:

在此处输入图片说明

I need to get each line into a unique string inside a List(Of String) , and remove the separator characters the user has entered ( "," ). Pretty easy using:

Dim a As String = txt.Text.Trim.Replace(",", String.Empty)
Dim l As List(Of String) = a.Split(Environment.NewLine).ToList()
For Each s As String In l
    Trace.Warn(String.Format("Parsed {0}: {1}", l.IndexOf(s).ToString, s))
Next

On examining the trace output however, there are additional line breaks that still exist. The debugger also shows their existence:

在此处输入图片说明

I assume this is because Environment.NewLine is not the correct choice for the separator. If so, why? - it clearly is a new line!

What is actually happening here under the hood, and what's the correct solution?

UPDATE

Comparing the contents of the string to the CHR(n) equivalent representation, the following output reveals what's actually contained in the box, Chr(10) plus Chr(13) :

在此处输入图片说明

It seems that Chr(10) and Chr(13) behave independently of one another. The solution was to Split using Environment.NewLine and then iterate the list and manually removing the remaining Chr(10) characters that remained.

Dim a As String = txt.Text.Trim.Replace(",", String.Empty)
Dim l As List(Of String) = a.Split(Environment.NewLine).ToList()
For i As Integer = 0 To l.Count - 1
    l(i) = l(i).Replace(Chr(10), String.Empty)
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM