string 如何在 vb.net 中拆分字符串中的新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14795943/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:47:54  来源:igfitidea点击:

How to split new line in string in vb.net

vb.netstringsplit

提问by Adham

for example .. If I have a text like this

例如..如果我有这样的文字

214asd
df5df8
d66f66

I want to split them into 3 strings using vb.net .

我想使用 vb.net 将它们分成 3 个字符串。

回答by Oded

Assuming you want to split on new lines - using String.Splitwill return an array containing the parts:

假设您想在新行上拆分 - usingString.Split将返回一个包含这些部分的数组:

Dim parts As String() = myString.Split(new String() {Environment.NewLine},
                                       StringSplitOptions.None)

This will be platform specific, so you may want to split on "\n", "\r", "\n\r"or a combination of them. String.Splithas an overload that takes a string array with the strings you wish to split on.

这将是平台特定的,所以你可能想拆就"\n""\r""\n\r"或它们的组合。String.Split有一个重载,它接受一个字符串数组,其中包含您希望拆分的字符串。

回答by Cory Hymanson

Dim strLines() As String = Clipboard.GetText.Replace(Chr(13), "").Split(Chr(10))

I like doing it this way. One can only split on a char but in most cases NewLine is two characters, Carriage Return (0x0D AKA Char 13) and Line Feed (0x0A AKA Char 10). But in other systems it's just a LF. So I simply remove all instances of the CR and split on the LF.

我喜欢这样做。一个只能在一个字符上拆分,但在大多数情况下,NewLine 是两个字符,回车(0x0D AKA Char 13)和换行符(0x0A AKA Char 10)。但在其他系统中,它只是一个 LF。所以我只是删除了 CR 的所有实例并在 LF 上拆分。

回答by scartag

str.Split(New String() {Environment.NewLine}, 
          StringSplitOptions.RemoveEmptyEntries)

回答by Geoffrey Lee

Dim enter As String = vbCrLf
Dim linecount As Integer = 0
Dim sr As New System.IO.StreamReader(yourTextFilePath)
linecount = sr.ReadToEnd.Split(CChar(enter)).Length - 1