通过子字符串拆分字符串而不是char

时间:2021-04-21 19:19:17

I have searched the site before asking but none of the solution I've found make sense with my problem.

我在询问之前搜索了网站,但我找到的解决方案都没有对我的问题有意义。

Firstly I have a string which is something like:

首先我有一个字符串,如下所示:

Dim someString As String = "<object>sometext</object><object>sometext</object><object>sometext</object>"

I am trying to get it split into an array of string like so:

我试图将它拆分成一个字符串数组,如下所示:

stringArray 0 --> "<object>sometext</object>"
stringArray 1 --> "<object>sometext</object>"
stringArray 2 --> "<object>sometext</object>"

The methods I have tried for splitting the text remove the delimiter from the result which is not what I want since it then invalidates the code.

我尝试拆分文本的方法从结果中删除了分隔符,这不是我想要的,因为它会使代码无效。

2 个解决方案

#1


3  

This is a bit pseudo code as I'm not sure of the exact VB syntax, but could you try forcing a character at the end of each so something like:

这是一个伪代码,因为我不确定VB的确切语法,但你可以尝试在每个结尾处强制一个字符,如下所示:

Dim someString As String = "<object>sometext</object><object>sometext</object><object>sometext</object>"
var stringToSpilt = someString.Replace("</object>","</object>~")
var splitObjects = stringToSpilt.Split("~")

If the "object" could be any text then RegEx replace would do this, but as the guys above have commented, if it's XML splitting it'll be more involved than this.

如果“对象”可以是任何文本,那么RegEx替换会做到这一点,但正如上面的人已经评论过,如果它是XML分裂它将比这更多涉及。

#2


0  

Using IndexOf and working with a List(Of String), no hidden strings fragmentation

使用IndexOf并使用List(Of String),没有隐藏的字符串碎片

Dim someString As String = "<object>sometext</object><object>sometext</object><object>sometext</object>"
Dim result = new List(Of String)()
Dim pos = 0
Dim init = 0
Dim len = "</object>".Length
do 
   pos = someString.IndexOf("</object>", init)
   if pos <> -1 Then
      result.Add(someString.Substring(init, pos +  len - init))
      init = pos + len + 1
   End If
Loop while pos <> -1

#1


3  

This is a bit pseudo code as I'm not sure of the exact VB syntax, but could you try forcing a character at the end of each so something like:

这是一个伪代码,因为我不确定VB的确切语法,但你可以尝试在每个结尾处强制一个字符,如下所示:

Dim someString As String = "<object>sometext</object><object>sometext</object><object>sometext</object>"
var stringToSpilt = someString.Replace("</object>","</object>~")
var splitObjects = stringToSpilt.Split("~")

If the "object" could be any text then RegEx replace would do this, but as the guys above have commented, if it's XML splitting it'll be more involved than this.

如果“对象”可以是任何文本,那么RegEx替换会做到这一点,但正如上面的人已经评论过,如果它是XML分裂它将比这更多涉及。

#2


0  

Using IndexOf and working with a List(Of String), no hidden strings fragmentation

使用IndexOf并使用List(Of String),没有隐藏的字符串碎片

Dim someString As String = "<object>sometext</object><object>sometext</object><object>sometext</object>"
Dim result = new List(Of String)()
Dim pos = 0
Dim init = 0
Dim len = "</object>".Length
do 
   pos = someString.IndexOf("</object>", init)
   if pos <> -1 Then
      result.Add(someString.Substring(init, pos +  len - init))
      init = pos + len + 1
   End If
Loop while pos <> -1