根据特定要求获取字符串的特定字符串部分 [英] Take specific string part of string by specific requirments

查看:26
本文介绍了根据特定要求获取字符串的特定字符串部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发可以为我提供特定字符串右侧的函数.函数有可能基于拆分的字符获取返回字符串,将字符保留在字符串中,并且最重要的是说返回字符串将在该字符分隔符的最后一次出现之后,或者说在字符串中分隔符出现的确切位置之后,返回串后取.任何人都可以看看并确认它是否正确或该代码是否包含任何问题?

I am developing function which would give me the right side of specific string. Function has possibility to take the return string based on splitted char, keep char in string and also which is most important to say either return string will be after last occurence of that char separator or say after exactly which positon of separator occerence in string, return string after to take. Can anyone take a look on that and confirm is it right way to go or whether that code contains any issues?

请记住,当用作 lastindex 时,我将 lastindexof 设置为 true 并且 splitterCharPosition 是什么并不重要,反之亦然,当必须设置 false splitterCharPosition 时.

Keep in mind to use as lastindex i set lastindexof to true and doesn't matter what splitterCharPosition is and vice versa when false splitterCharPosition has to be set.

这是我当前要确认的代码:

This is my current code to be confirmed:

 Public Function GetRightSideStringByCHar(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

获取n个索引的辅助函数:

Helper function to get n index:

 Public 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

推荐答案

根据您其他已删除的问题,我为您制定了这个方案.

Based on your other deleted question, I formulated this for you.

它适用于您之前的所有示例.

It worked with all your previous examples.

[parameters]
str = input string
strtofind = separator string
occurance = zero-based index to start cutting (optional)
keepstringtofind = append separator to string (optional)
righttoleft = operational direction (optional)

    Public Function CutFromString(str As String, strtofind As String, Optional occurance As Integer = 0, Optional keepstrtofind As Boolean = False, Optional righttoleft As Boolean = False) As String
    Dim s As String = str
    Dim sections As List(Of String) = IIf(String.IsNullOrEmpty(str), New List(Of String), str.Split(strtofind.ToCharArray).ToList)
    If sections.Count = 1 AndAlso sections.First = str Then sections.Clear()
    If occurance < sections.Count Then
        Dim b = sections.Count - occurance
        While b > 0
            If righttoleft Then
                sections.RemoveAt(0)
            Else
                sections.RemoveAt(sections.Count - 1)
            End If
            b -= 1
        End While
        s = Join(sections.ToArray, strtofind)
        If keepstrtofind And occurance > 0 Then
            If righttoleft Then
                s = strtofind + s
            Else
                s += strtofind
            End If
        End If
    End If
    Return s
End Function

这篇关于根据特定要求获取字符串的特定字符串部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆