Text Line split visual basic [英] Text Line split visual basic
问题描述
如何分成两部分原始行?谢谢。
原始行= HD_PRS22_S_DL 33000
Textbox1因此= HD_PRS22_S_DL
Textbox2因此= 33000
不是:HD_PRS22_S_DL<一个标签空间> 33000
Hi,
How can I split in two parts Original line ? Thanks.
original line = HD_PRS22_S_DL 33000
Textbox1 thus = HD_PRS22_S_DL
Textbox2 thus = 33000
Not: HD_PRS22_S_DL < one tab space > 33000
推荐答案
我将使用的方法是字符串类上的共享Split方法。这个方法有几个重载,但有一个在intellisence建议中变得明显,因为它是一个特殊情况,split items数组为null(Nothing)。这告诉方法使用空白字符作为分割点。使用的确切字符列表取决于.Net框架的版本。对于.Net 4.0及更高版本,它与 Char.IsWhiteSpace方法 [ ^ ]对于.Net 3.5及以下版本,请参阅这里的备注部分 [ ^ ]。
The method that I would use is the shared Split method on the string class. This method has several overloads, but there is one that does become apparent in the intellisence suggestions because it is a special case where the split items array is null (Nothing). This tells the method to use whitespace characters as the split points. The exact list of characters used depends on the version of the .Net framework. For .Net 4.0 and above its is the same list used by the Char.IsWhiteSpace method [^] For .Net 3.5 and below see this list in the remarks section here[^].
Dim str As String = "1" & vbLf & "2" & vbTab & "3" & vbCrLf & "4 5"
Dim parts() As String = str.Split(New Char() {}, System.StringSplitOptions.RemoveEmptyEntries)
部件将是一个包含五个字符串的数组。方法中的第二个参数(System.StringSplitOptions.RemoveEmptyEntries,告诉Split方法删除可以由连续的空格字符创建的e,mptry条目,例如示例代码中显示的VBCrLF(回车符和换行符)。
Parts will be an array comprising five strings. The second parameter in the method, (System.StringSplitOptions.RemoveEmptyEntries, tells the Split method to delete e,mptry entries that could be created by consecutive whitespace characters such as the VBCrLF (carriage return and linefeed) shown in the example code.
此示例代码假定单个空格是字符串的两个子部分之间的分隔符。
This sample code assumes that a single space is the delimiter between the two subparts of the string.
Dim strInput As String = "HD_PRS22_S_DL 33000"
Dim x As Integer = strInput.IndexOf(" ") ' Assumes space is the delimiter
Console.WriteLine(strInput.Substring(0, x))
Console.WriteLine(strInput.Substring(x + 1))
此示例代码假定单个Tab字符是字符串的两个子部分之间的分隔符。
This sample code assumes that a single Tab character is the delimiter between the two subparts of the string.
Dim strInput As String = "HD_PRS22_S_DL" & vbTab & "33000"
Dim x As Integer = strInput.IndexOf(vbTab) ' Assumes Tab is the delimiter
Console.WriteLine(strInput.Substring(0, x))
Console.WriteLine(strInput.Substring(x + 1))
此示例代码假定一个或多个空格是字符串的两个子部分之间的分隔符。
This sample code assumes that one or more spaces are the delimiter between the two subparts of the string.
Dim strInput As String = "HD_PRS22_S_DL" & SPACE(5) & "33000"
Dim x As Integer = strInput.IndexOf(" ") ' Assumes space is the delimiter
Console.WriteLine(strInput.Substring(0, x).Trim)
Console.WriteLine(strInput.Substring(x + 1).Trim)
此示例代码假定Tab字符是分隔符,但字符串的两个子部分之间可能有一个或多个空格。
This sample code assumes that a Tab character is the delimiter but there could be one or more spaces between the two subparts of the string.
Dim strInput As String = "HD_PRS22_S_DL" & SPACE(2) & vbTab & SPACE(2) & "33000"
Dim x As Integer = strInput.IndexOf(vbTab) ' Assumes Tab is the delimiter
Console.WriteLine(strInput.Substring(0, x).Trim)
Console.WriteLine(strInput.Substring(x + 1).Trim)
有很多方法可以实现它。看看这个:
www.dotnetperls.com/split-vbnet [ ^ ]
There are a number of ways to achieve it. Check this out:
www.dotnetperls.com/split-vbnet[^]
这篇关于Text Line split visual basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!