使用 VB.net 由 crlf 拆分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5826810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
split by crlf using VB.net
提问by user709787
Need help how to proper split a string by crlf below is the code:
需要帮助如何通过下面的 crlf 正确拆分字符串是代码:
Dim str As String = "Hello" & vbCrLf & "World"
Dim parts As String() = str.Split(ControlChars.CrLf.ToCharArray)
For Each part As String In parts
MsgBox(part)
Next
Output
输出
Hello
World
I want to get rid the blank space in between the two.
我想摆脱两者之间的空白。
Hello
World
你好
世界
回答by Ry-
Use
用
str.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
instead.
反而。
回答by KekuSemau
The given answer splits on any cr
OR lf
and removes blanks; that works ok for the given case, but it would remove 'real' empty lines as well (and feels unclean to me).
给定的答案在任何cr
OR上拆分lf
并删除空格;这适用于给定的情况,但它也会删除“真正的”空行(并且对我来说感觉不干净)。
Alternative:
选择:
System.Text.RegularExpressions.Regex.Split(str, vbCrLf)
(note that the second string is a regex-pattern, special chars would have to be escaped)
(请注意,第二个字符串是正则表达式模式,必须转义特殊字符)
回答by D_Bester
This code will split on line-feed, new line or both(vbCrLf). It will not remove empty lines.
此代码将在换行、换行或两者(vbCrLf)上拆分。它不会删除空行。
Dim arr() As String = System.Text.RegularExpressions.Regex.Split(text, "(\r\n|\r|\n)", _
RegularExpressions.RegexOptions.ExplicitCapture)