将字符串拆分为多个部分

时间:2015-10-19 01:03:45

标签: .net vb.net split string-function

我正在尝试将一个字符串分成几部分,但无法解决它!

我的主要观点来自一个字符串

from celery import Celery as _Celery


class Celery(_Celery)

    def get_message_count(self, queue):
        '''
        Raises: amqp.exceptions.NotFound: if queue does not exist
        '''
        with self.connection_or_acquire() as conn:
            return conn.default_channel.queue_declare(
                queue=queue, passive=True).message_count


celery = Celery(app)
num_messages = celery.get_message_count('my-queue')

"hello bye see you"

读取

我试过

"bye" to "you"

但是我被困在代码的其他部分,我真的想要一些帮助。 对不起,如果我不是最好的解释事情,至少我尽力了:/

1 个答案:

答案 0 :(得分:1)

我假设您的预期输出为bye see you。如果我理解正确,那么可以使用方法来获得所需的输出:

在此字符串中拆分为带有分隔符splits()的数组(" "),并找到索引byej)和you({{1然后在数组中使用kfor loopbye之间的数组中获取字符串。

you

用法:

Function GETSTRINGBETWEEN(ByVal start As String, ByVal parent As String, ByVal [end] As String)
        Dim output As String = ""
        Dim splits() As String = parent.Split(" ")
        Dim i As Integer
        Dim j As Integer = Array.IndexOf(splits, start)
        Dim k As Integer = Array.IndexOf(splits, [end])
        For i = j To k
            If output = String.Empty Then
                output = splits(i)
            Else
                output = output & " " & splits(i)
            End If
        Next
        Return output
    End Function
Dim val As String
val = GETSTRINGBETWEEN("bye", "hello bye see you", "you")
'val="bye see you"

用法:

Function GET_STRING_BETWEEN(ByVal start As String, ByVal parent As String, ByVal [end] As String)
        Dim output As String
        output = parent.Substring(parent.IndexOf(start) _
                                                , (parent.IndexOf([end]) _
                                                   - parent.IndexOf(start)) _
                                                   ).Replace(start, "").Replace([end], "")
        output = start & output & [end]
        Return output
    End Function