Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET:确定输入文本字符串中的分隔符_Vb.net_String_Text_Textbox_Delimiter - Fatal编程技术网

VB.NET:确定输入文本字符串中的分隔符

VB.NET:确定输入文本字符串中的分隔符,vb.net,string,text,textbox,delimiter,Vb.net,String,Text,Textbox,Delimiter,有没有办法主动扫描文本框以搜索键分隔符?我正在寻找分隔符:vbNewLine、逗号、冒号、分号和空格 一旦用户粘贴了他们的分隔字符串,我如何让VB.net确定哪个字符正在分隔 例如: 输入字符串:“aaa、bbb、ccc” 我希望能够扫描该文本,并知道分隔符是“,” 与“aaa;bbb;ccc”相同,将为“ 分隔符之间可以有任意数量的值您可以使用以下类进行此操作,ClosestDelimiter为您提供最有可能匹配的分隔符(如果它找不到它要查找的内容,它将抛出一个错误,警告:) 我还添加了一个P

有没有办法主动扫描文本框以搜索键分隔符?我正在寻找分隔符:vbNewLine、逗号、冒号、分号和空格

一旦用户粘贴了他们的分隔字符串,我如何让VB.net确定哪个字符正在分隔

例如:

输入字符串:“aaa、bbb、ccc”

我希望能够扫描该文本,并知道分隔符是“,”

与“aaa;bbb;ccc”相同,将为“


分隔符之间可以有任意数量的值

您可以使用以下类进行此操作,
ClosestDelimiter
为您提供最有可能匹配的分隔符(如果它找不到它要查找的内容,它将抛出一个错误,警告:)

我还添加了一个
Parse
方法,该方法返回单个项的数组(可以包含空字符串,以防在前一个分隔符后面直接出现分隔符,例如:;a;;C;;D)

最后是一个
TryParse
方法,它返回true或false,并为您捕获异常,在解析失败时将结果更新为Nothing

Public Class DelimiterScanner
    Private delimiters() As String = {vbNewLine, ",", ";", ":", " "}

    Public Function ClosestDelimiter(row As String) As String
        Dim maxUsages As Integer = 0
        Dim multiplePossibilities As Boolean = False
        Dim current As Integer
        Dim delimiter As String = Nothing

        If String.IsNullOrWhiteSpace(row) Then
            Throw New ArgumentException("Row cannot be an empty string", "row")
        End If

        For Each del As String In delimiters
            current = row.Split({del}, StringSplitOptions.None).Count
            If current > maxUsages Then
                delimiter = del
                maxUsages = current
                multiplePossibilities = False
            ElseIf current > 0 AndAlso current = maxUsages Then
                multiplePossibilities = True
            End If
        Next
        If multiplePossibilities Then
            Throw New FormatException("Multiple delimiters have the same length")
        End If
        If maxUsages = 0 Then
            Throw New FormatException("No delimiters found in row")
        End If
        Return delimiter
    End Function

    Public Function Parse(row As String, Optional useDelimiter As String = Nothing) As String()
        Dim selectedDelimiter As String = useDelimiter
        Dim result() As String = Nothing

        If String.IsNullOrWhiteSpace(row) Then
            ' don't parse empty strings
            Throw New ArgumentException("Cannot parse an empty input value", row)
        End If
        If String.IsNullOrWhiteSpace(selectedDelimiter) Then
            ' get delimiter if no basic is given
            selectedDelimiter = ClosestDelimiter(row)
        End If
        result = row.Split({selectedDelimiter}, StringSplitOptions.None)

        Return result
    End Function

    Public Function TryParse(row As String, ByRef result() As String, Optional useDelimiter As String = Nothing) As Boolean
        Dim succeeded As Boolean = True

        Try
            result = Parse(row, useDelimiter)
        Catch ex As Exception
            result = Nothing
            succeeded = False
        End Try
        Return succeeded

    End Function
End Class
更新

例如,您可以像这样使用这个类(这里您可以看到我的控制台类的main方法)。我们首先启动new类,然后使用
TryParse
方法检查它是否可以解析,否则解析无效,并且无法确定分隔符。如果有效,则可以调用
ClosestDelimiter
方法,还可以假设结果包含正确的字符串

我还对SplitMethod进行了更新,因为我必须考虑到我们希望在字符串上拆分,而不是在单独的字符上拆分(例如:VbNewLine),我在上面的代码中也纠正了这一点

Sub Main()
    Dim dScanner As New DelimiterScanner()
    Dim result() As String = Nothing
    Dim tests() As String = {String.Empty, "a,b;c", "aaa,bbb,ccc;", "a;b;c;;;;d", "this is a test;working;online;stackoverflow;", "aaa" + vbNewLine + "bbb" + vbNewLine + "ccc"}

    For Each testItem In tests
        Console.WriteLine("Trying to parse {0}", testItem)
        If dScanner.TryParse(testItem, result) Then
            Console.WriteLine(vbTab & "- Results (joined with ,): {0}", String.Join(",", result))
            Console.WriteLine(vbTab & "- Used delimited: {0}", dScanner.ClosestDelimiter(testItem))
        Else
            Console.WriteLine(vbTab & "- Delimiter cannot be found!")
        End If
    Next
    Console.ReadLine()
End Sub
然后输出如下所示:

Trying to parse
        - Delimiter cannot be found!
Trying to parse a,b;c
        - Delimiter cannot be found!
Trying to parse aaa,bbb,ccc;
        - Results (joined with ,): aaa,bbb,ccc;
        - Used delimited: ,
Trying to parse a;b;c;;;;d
        - Results (joined with ,): a,b,c,,,,d
        - Used delimited: ;
Trying to parse this is a test;working;online;stackoverflow;
        - Results (joined with ,): this is a test,working,online,stackoverflow,
        - Used delimited: ;
Trying to parse aaa
bbb
ccc
        - Results (joined with ,): aaa,bbb,ccc
        - Used delimited:

希望这对您有所帮助:)

您可以使用以下类来完成此操作,
ClosestDelimiter
为您提供了最有可能匹配的分隔符(如果它没有找到它要查找的内容,它将抛出一个错误,请注意:))

我还添加了一个
Parse
方法,该方法返回单个项的数组(可以包含空字符串,以防在前一个分隔符后面直接出现分隔符,例如:;a;;C;;D)

最后是一个
TryParse
方法,它返回true或false,并为您捕获异常,在解析失败时将结果更新为Nothing

Public Class DelimiterScanner
    Private delimiters() As String = {vbNewLine, ",", ";", ":", " "}

    Public Function ClosestDelimiter(row As String) As String
        Dim maxUsages As Integer = 0
        Dim multiplePossibilities As Boolean = False
        Dim current As Integer
        Dim delimiter As String = Nothing

        If String.IsNullOrWhiteSpace(row) Then
            Throw New ArgumentException("Row cannot be an empty string", "row")
        End If

        For Each del As String In delimiters
            current = row.Split({del}, StringSplitOptions.None).Count
            If current > maxUsages Then
                delimiter = del
                maxUsages = current
                multiplePossibilities = False
            ElseIf current > 0 AndAlso current = maxUsages Then
                multiplePossibilities = True
            End If
        Next
        If multiplePossibilities Then
            Throw New FormatException("Multiple delimiters have the same length")
        End If
        If maxUsages = 0 Then
            Throw New FormatException("No delimiters found in row")
        End If
        Return delimiter
    End Function

    Public Function Parse(row As String, Optional useDelimiter As String = Nothing) As String()
        Dim selectedDelimiter As String = useDelimiter
        Dim result() As String = Nothing

        If String.IsNullOrWhiteSpace(row) Then
            ' don't parse empty strings
            Throw New ArgumentException("Cannot parse an empty input value", row)
        End If
        If String.IsNullOrWhiteSpace(selectedDelimiter) Then
            ' get delimiter if no basic is given
            selectedDelimiter = ClosestDelimiter(row)
        End If
        result = row.Split({selectedDelimiter}, StringSplitOptions.None)

        Return result
    End Function

    Public Function TryParse(row As String, ByRef result() As String, Optional useDelimiter As String = Nothing) As Boolean
        Dim succeeded As Boolean = True

        Try
            result = Parse(row, useDelimiter)
        Catch ex As Exception
            result = Nothing
            succeeded = False
        End Try
        Return succeeded

    End Function
End Class
更新

例如,您可以像这样使用这个类(这里您可以看到我的控制台类的main方法)。我们首先启动new类,然后使用
TryParse
方法检查它是否可以解析,否则解析无效,并且无法确定分隔符。如果有效,则可以调用
ClosestDelimiter
方法,还可以假设结果包含正确的字符串

我还对SplitMethod进行了更新,因为我必须考虑到我们希望在字符串上拆分,而不是在单独的字符上拆分(例如:VbNewLine),我在上面的代码中也纠正了这一点

Sub Main()
    Dim dScanner As New DelimiterScanner()
    Dim result() As String = Nothing
    Dim tests() As String = {String.Empty, "a,b;c", "aaa,bbb,ccc;", "a;b;c;;;;d", "this is a test;working;online;stackoverflow;", "aaa" + vbNewLine + "bbb" + vbNewLine + "ccc"}

    For Each testItem In tests
        Console.WriteLine("Trying to parse {0}", testItem)
        If dScanner.TryParse(testItem, result) Then
            Console.WriteLine(vbTab & "- Results (joined with ,): {0}", String.Join(",", result))
            Console.WriteLine(vbTab & "- Used delimited: {0}", dScanner.ClosestDelimiter(testItem))
        Else
            Console.WriteLine(vbTab & "- Delimiter cannot be found!")
        End If
    Next
    Console.ReadLine()
End Sub
然后输出如下所示:

Trying to parse
        - Delimiter cannot be found!
Trying to parse a,b;c
        - Delimiter cannot be found!
Trying to parse aaa,bbb,ccc;
        - Results (joined with ,): aaa,bbb,ccc;
        - Used delimited: ,
Trying to parse a;b;c;;;;d
        - Results (joined with ,): a,b,c,,,,d
        - Used delimited: ;
Trying to parse this is a test;working;online;stackoverflow;
        - Results (joined with ,): this is a test,working,online,stackoverflow,
        - Used delimited: ;
Trying to parse aaa
bbb
ccc
        - Results (joined with ,): aaa,bbb,ccc
        - Used delimited:

希望这对您有所帮助:)

您可以使用以下类来完成此操作,
ClosestDelimiter
为您提供了最有可能匹配的分隔符(如果它没有找到它要查找的内容,它将抛出一个错误,请注意:))

我还添加了一个
Parse
方法,该方法返回单个项的数组(可以包含空字符串,以防在前一个分隔符后面直接出现分隔符,例如:;a;;C;;D)

最后是一个
TryParse
方法,它返回true或false,并为您捕获异常,在解析失败时将结果更新为Nothing

Public Class DelimiterScanner
    Private delimiters() As String = {vbNewLine, ",", ";", ":", " "}

    Public Function ClosestDelimiter(row As String) As String
        Dim maxUsages As Integer = 0
        Dim multiplePossibilities As Boolean = False
        Dim current As Integer
        Dim delimiter As String = Nothing

        If String.IsNullOrWhiteSpace(row) Then
            Throw New ArgumentException("Row cannot be an empty string", "row")
        End If

        For Each del As String In delimiters
            current = row.Split({del}, StringSplitOptions.None).Count
            If current > maxUsages Then
                delimiter = del
                maxUsages = current
                multiplePossibilities = False
            ElseIf current > 0 AndAlso current = maxUsages Then
                multiplePossibilities = True
            End If
        Next
        If multiplePossibilities Then
            Throw New FormatException("Multiple delimiters have the same length")
        End If
        If maxUsages = 0 Then
            Throw New FormatException("No delimiters found in row")
        End If
        Return delimiter
    End Function

    Public Function Parse(row As String, Optional useDelimiter As String = Nothing) As String()
        Dim selectedDelimiter As String = useDelimiter
        Dim result() As String = Nothing

        If String.IsNullOrWhiteSpace(row) Then
            ' don't parse empty strings
            Throw New ArgumentException("Cannot parse an empty input value", row)
        End If
        If String.IsNullOrWhiteSpace(selectedDelimiter) Then
            ' get delimiter if no basic is given
            selectedDelimiter = ClosestDelimiter(row)
        End If
        result = row.Split({selectedDelimiter}, StringSplitOptions.None)

        Return result
    End Function

    Public Function TryParse(row As String, ByRef result() As String, Optional useDelimiter As String = Nothing) As Boolean
        Dim succeeded As Boolean = True

        Try
            result = Parse(row, useDelimiter)
        Catch ex As Exception
            result = Nothing
            succeeded = False
        End Try
        Return succeeded

    End Function
End Class
更新

例如,您可以像这样使用这个类(这里您可以看到我的控制台类的main方法)。我们首先启动new类,然后使用
TryParse
方法检查它是否可以解析,否则解析无效,并且无法确定分隔符。如果有效,则可以调用
ClosestDelimiter
方法,还可以假设结果包含正确的字符串

我还对SplitMethod进行了更新,因为我必须考虑到我们希望在字符串上拆分,而不是在单独的字符上拆分(例如:VbNewLine),我在上面的代码中也纠正了这一点

Sub Main()
    Dim dScanner As New DelimiterScanner()
    Dim result() As String = Nothing
    Dim tests() As String = {String.Empty, "a,b;c", "aaa,bbb,ccc;", "a;b;c;;;;d", "this is a test;working;online;stackoverflow;", "aaa" + vbNewLine + "bbb" + vbNewLine + "ccc"}

    For Each testItem In tests
        Console.WriteLine("Trying to parse {0}", testItem)
        If dScanner.TryParse(testItem, result) Then
            Console.WriteLine(vbTab & "- Results (joined with ,): {0}", String.Join(",", result))
            Console.WriteLine(vbTab & "- Used delimited: {0}", dScanner.ClosestDelimiter(testItem))
        Else
            Console.WriteLine(vbTab & "- Delimiter cannot be found!")
        End If
    Next
    Console.ReadLine()
End Sub
然后输出如下所示:

Trying to parse
        - Delimiter cannot be found!
Trying to parse a,b;c
        - Delimiter cannot be found!
Trying to parse aaa,bbb,ccc;
        - Results (joined with ,): aaa,bbb,ccc;
        - Used delimited: ,
Trying to parse a;b;c;;;;d
        - Results (joined with ,): a,b,c,,,,d
        - Used delimited: ;
Trying to parse this is a test;working;online;stackoverflow;
        - Results (joined with ,): this is a test,working,online,stackoverflow,
        - Used delimited: ;
Trying to parse aaa
bbb
ccc
        - Results (joined with ,): aaa,bbb,ccc
        - Used delimited:

希望这对您有所帮助:)

您可以使用以下类来完成此操作,
ClosestDelimiter
为您提供了最有可能匹配的分隔符(如果它没有找到它要查找的内容,它将抛出一个错误,请注意:))

我还添加了一个
Parse
方法,该方法返回单个项的数组(可以包含空字符串,以防在前一个分隔符后面直接出现分隔符,例如:;a;;C;;D)

最后是一个
TryParse
方法,它返回true或false,并为您捕获异常,在解析失败时将结果更新为Nothing

Public Class DelimiterScanner
    Private delimiters() As String = {vbNewLine, ",", ";", ":", " "}

    Public Function ClosestDelimiter(row As String) As String
        Dim maxUsages As Integer = 0
        Dim multiplePossibilities As Boolean = False
        Dim current As Integer
        Dim delimiter As String = Nothing

        If String.IsNullOrWhiteSpace(row) Then
            Throw New ArgumentException("Row cannot be an empty string", "row")
        End If

        For Each del As String In delimiters
            current = row.Split({del}, StringSplitOptions.None).Count
            If current > maxUsages Then
                delimiter = del
                maxUsages = current
                multiplePossibilities = False
            ElseIf current > 0 AndAlso current = maxUsages Then
                multiplePossibilities = True
            End If
        Next
        If multiplePossibilities Then
            Throw New FormatException("Multiple delimiters have the same length")
        End If
        If maxUsages = 0 Then
            Throw New FormatException("No delimiters found in row")
        End If
        Return delimiter
    End Function

    Public Function Parse(row As String, Optional useDelimiter As String = Nothing) As String()
        Dim selectedDelimiter As String = useDelimiter
        Dim result() As String = Nothing

        If String.IsNullOrWhiteSpace(row) Then
            ' don't parse empty strings
            Throw New ArgumentException("Cannot parse an empty input value", row)
        End If
        If String.IsNullOrWhiteSpace(selectedDelimiter) Then
            ' get delimiter if no basic is given
            selectedDelimiter = ClosestDelimiter(row)
        End If
        result = row.Split({selectedDelimiter}, StringSplitOptions.None)

        Return result
    End Function

    Public Function TryParse(row As String, ByRef result() As String, Optional useDelimiter As String = Nothing) As Boolean
        Dim succeeded As Boolean = True

        Try
            result = Parse(row, useDelimiter)
        Catch ex As Exception
            result = Nothing
            succeeded = False
        End Try
        Return succeeded

    End Function
End Class
更新

例如,您可以像这样使用这个类(这里您可以看到我的控制台类的main方法)。我们首先启动new类,然后使用
TryParse
方法检查它是否可以解析,否则解析无效,并且无法确定分隔符。如果有效,则可以调用
ClosestDelimiter
方法,还可以假设结果包含正确的字符串

我还对SplitMethod进行了更新,因为我必须考虑到我们希望在字符串上拆分,而不是在单独的字符上拆分(例如:VbNewLine),我在上面的代码中也纠正了这一点

Sub Main()
    Dim dScanner As New DelimiterScanner()
    Dim result() As String = Nothing
    Dim tests() As String = {String.Empty, "a,b;c", "aaa,bbb,ccc;", "a;b;c;;;;d", "this is a test;working;online;stackoverflow;", "aaa" + vbNewLine + "bbb" + vbNewLine + "ccc"}

    For Each testItem In tests
        Console.WriteLine("Trying to parse {0}", testItem)
        If dScanner.TryParse(testItem, result) Then
            Console.WriteLine(vbTab & "- Results (joined with ,): {0}", String.Join(",", result))
            Console.WriteLine(vbTab & "- Used delimited: {0}", dScanner.ClosestDelimiter(testItem))
        Else
            Console.WriteLine(vbTab & "- Delimiter cannot be found!")
        End If
    Next
    Console.ReadLine()
End Sub
然后输出如下所示:

Trying to parse
        - Delimiter cannot be found!
Trying to parse a,b;c
        - Delimiter cannot be found!
Trying to parse aaa,bbb,ccc;
        - Results (joined with ,): aaa,bbb,ccc;
        - Used delimited: ,
Trying to parse a;b;c;;;;d
        - Results (joined with ,): a,b,c,,,,d
        - Used delimited: ;
Trying to parse this is a test;working;online;stackoverflow;
        - Results (joined with ,): this is a test,working,online,stackoverflow,
        - Used delimited: ;
Trying to parse aaa
bbb
ccc
        - Results (joined with ,): aaa,bbb,ccc
        - Used delimited:

希望这有帮助:)

标准是什么?在3个字符的字符串中
,哪个应该被视为分隔符?对不起,添加了mo