0

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.

Greg
  • 187
  • 3
  • 15
  • 1
    I presume you want to parse XML so. That would make your task much easier. Here's some reading on using XML in VB.net http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET – Lorcan O'Neill Jan 15 '14 at 16:29
  • Related: http://stackoverflow.com/a/1732454/1440433 – Joker_vD Jan 15 '14 at 16:30
  • Hi thanks for the reply. The string is actually stored in a database like this so it's not from an xml file. It seems like a lot of extra code to use XML just to split them – Greg Jan 15 '14 at 16:48

2 Answers2

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:

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.

Steve Newstead
  • 1,223
  • 2
  • 10
  • 20
0

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

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
Steve
  • 213,761
  • 22
  • 232
  • 286