提取字符串的右侧由分隔符指定

时间:2017-05-29 12:44:23

标签: vb.net

这是我的代码,用于获取指定字符分隔符的字符串的右侧,以及是否将分隔符保留在字符串中。也可以指定是否只是最后出现的char分隔符或手动定义它。我的问题是如何制作相同版本,但这次是为了获得字符串的右侧而不是左侧?

Public Shared Function GetLetSideStringByChar(splitterChar As String, searchingWord As String, keepCharAsWell As Boolean, lastindexof As Boolean, splitterCharPosition As Integer) As String
        Dim index As Integer
        Select Case lastindexof
            Case False
                index = GetNthIndex(searchingWord, splitterChar, splitterCharPosition)
            Case True
                index = searchingWord.LastIndexOf(splitterChar)
        End Select

        If index > 0 Then
            If keepCharAsWell Then
                searchingWord = searchingWord.Substring(0, index + splitterChar.Length)
            Else
                searchingWord = searchingWord.Substring(0, index)
            End If
        Else
            searchingWord = String.Empty
        End If
        Return searchingWord
    End Function

    'jesli n separator nie odnalzeiony bedzie return -1, np jesli charseparator = . i damy n = 2 a word bedzie mial tlko jedna . to -1
    Public Shared Function GetNthIndex(searchingWord As String, charseparator As Char, n As Integer) As Integer
        Dim count As Integer = 0
        For i As Integer = 0 To searchingWord.Length - 1
            If searchingWord(i) = charseparator Then
                count += 1
                If count = n Then
                    Return i
                End If
            End If
        Next
        Return -1
    End Function

1 个答案:

答案 0 :(得分:0)

我为您之前删除的问题编写了代码。 为了使代码更易于理解,我使用Enum来指定右侧或左侧:

Public Enum Direction
    Left = 0
    Right = 1
End Enum

然后你可以这样调用这个函数:

Console.WriteLine(StringExtract("5345.342.323.323#$%", Direction.Right, 2, False))

此处分隔符索引表示点号(从1开始)

例如:

  

648674.2327.12第一个点 1 2

这是功能,我确信它可以缩短:

Public Function StringExtract(ByVal MyStr As String, ByVal Side As Direction, ByVal SeperatorIndex As Integer, ByVal SeperatorKeep As Boolean) As String
    Dim MySubs() As String = MyStr.Split(".".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
    Dim IndexOfSplit As Integer
    Dim MyResult As String = ""
    If Side = Direction.Left Then
        IndexOfSplit = SeperatorIndex - 1
        For i As Integer = IndexOfSplit To 0 Step -1
            MyResult = MyResult.Insert(0, MySubs(i) & ".")
        Next
        If SeperatorKeep = False Then
            MyResult = MyResult.Remove(MyResult.LastIndexOf("."), 1)
        End If
    Else
        IndexOfSplit = SeperatorIndex
        For i As Integer = IndexOfSplit To MySubs.Length - 1
            MyResult = MyResult & MySubs(i) & "."
        Next
        If SeperatorKeep = False Then
            MyResult = MyResult.Remove(MyResult.LastIndexOf("."), 1)
        Else
            MyResult = "." & MyResult.Remove(MyResult.LastIndexOf("."), 1)
        End If
    End If
    Return MyResult
End Function

输出示例:

输入1:

StringExtract("5345.342.323.323#$%", Direction.Right, 2, False)

输出1:

  

323.323#$%

输入2:

StringExtract("5345.342.323.323#$%", Direction.Right, 3, True)

输出2:

  

0.323#$%

输入3:

StringExtract(" 4.34!2.3323。",Direction.Left,2,True)

输出3:

  

4.34!2。

PS:如果有人有任何缩短功能的建议,请告诉我,我很乐意学习。