将一个字符串分成几部分 [英] Split a string into parts

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

问题描述

我试图将一个字符串分成几部分,但无法弄清楚!

I am trying to split a string into parts but can't figure it out!

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

My main point is from a string

"hello bye see you" 

再见"到你"

我试过了

 Dim qnew() As String = tnew.Split(" ")

但是我在代码的其他部分卡住了,我真的很想得到一些帮助.对不起,如果我不擅长解释事情,至少我尽力了:/

But I got stuck on other parts of the code, I would really like some help. Sorry if I'm not the best at explaining things, at least I tried my best :/

推荐答案

我假设你的预期输出是再见.如果我理解正确,那么按照方法可以用于获得所需的输出:

I assume that your expected output is bye see you.If I understood correctly then following methods can be used to get the desired output:

在这个字符串中分割成一个数组(splits()) 与分隔符" " 并找到bye (j) 和 you(k) 然后使用 for 循环 来获取 bye 之间的数组中的字符串.

In this string splits into an array(splits()) with delimiter " " and find index of bye (j)and you(k) in the array then using a for loop to get strings in the array between bye and 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

用法:

Dim val As String
val = GET_STRING_BETWEEN("bye", "hello bye see you", "you")
'val="bye see you"

这篇关于将一个字符串分成几部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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