locked
extract sentences/split string base on keywords RRS feed

  • Question

  • Hi.

    i am trying to achieve the following below

    Current: Path(alldirs,nofiles)AccountOwnDirFileR:\data\R:\cddata\MS\OWDOCASD\AdministratorsoallallR:\cddata\MS\owasdasc\awdasdasdR:\cddata\.........

    I have filter and remove all whitespaces using the code below to achieve the following Current path

           Dim regx As New Regex("\s*")
            Dim inputStr As String = RichTextBox1.Text
            Dim outputStr As String = regx.Replace(inputStr, "")

    I would like to split them begining from the following highlighted "R:\data" and store each of this directory into an arraylist accordingly

    To-Be

    index (0) R:\data
    index (1) R:\cddata\MS\OWDOCASD\Administratorsoallall
    index (2) R:\cddata\MS\owasdasc\awdasdasd
    index (3) so on and so for......

    I have tried to use Regex (regular expression but it seems not working. to me sub string and InStr is hard as the path are not static and they are dynamic. 

    Hope to hear from you guys!

    Eric

    Tuesday, July 31, 2012 1:55 AM

Answers

  • Imports System.Collections.Generic
        Public Sub SplitTest()
            Dim StringToCheck As String = "R:\data\R:\cddata\MS\OWDOCASD\AdministratorsoallallR:\cddata\MS\owasdasc\awdasdasdR:\cddata"
            Dim StringToFind As String = "R:\"
            Dim Start As Integer = 1
            Dim Found As Integer = 0
            Dim Occurance As New List(Of String)
            While (Found >= 0)
                Found = StringToCheck.IndexOf(StringToFind, Start)
                If (Found > 0) Then
                    Occurance.Add(StringToCheck.Substring(Start - 1, Found - Start + 1))
                    Start = Found + 1
                End If
            End While
            Occurance.Add(StringToCheck.Substring(Start - 1, StringToCheck.Length - Start + 1))
            For Each s As String In Occurance
                Console.WriteLine(s)
            Next
            Console.ReadKey()
        End Sub

    Sample output:

    R:\data\
    R:\cddata\MS\OWDOCASD\Administratorsoallall
    R:\cddata\MS\owasdasc\awdasdasd
    R:\cddata


    Tuesday, July 31, 2012 7:53 AM
    Answerer

All replies

  • Will the following work for you?

    AccountOwnDirFile.Split(New String(){"R:\"}, StringSplitOptions.None)
    Just make sure before using the result, prepend "R:\" in the front.
    Tuesday, July 31, 2012 2:43 AM
    Answerer
  • Hi Cheong,

    It is not working i have managed to come out with a parital solution by looking into Instr and manage to determine the position for "R:\" as shown below

    Dim Occurrences As Integer
    Dim StringToCheck As String = RemoveSpaces
    Dim StringToFind As String = "R:\"

     Dim Start As Integer
            Dim Found As Integer
            Dim OccurancePosition As New ArrayList()
            Do
                Start = Found + 1
                Found = InStr(Start, StringToCheck, StringToFind)
            
                OccurancePosition.Add(Found)
                If Found = 0 Then Exit Do
                Occurrences += 1
            Loop

    however how can the substring work such that for example   substring (39, 49 -39)  --> Where

    [0] position - 39

    [1] position - 49

    Whereby, how do i retrieve the previous object from the arraylist without get index out of bound so that i can do the relevant substraction according to fit the substring parameter shown below

    Dim resultObjPrevious As Object
    Dim positionToStringPrevious As String
    Dim countcount As Integer = 0

    For search = 0 To OccurancePosition.Count - 1
                resultObj = OccurancePosition.Item(search)
                positionToString = resultObj.ToString()
                countcount += CInt(positiontoString)
                'positionToStringPrevious = resultObjPrevious.ToString()
                Dim extractDirectory As String = positionToString.Substring(CInt(positionToString),(CInt(positionToString) - CInt(countcount)))
            Next

    Tuesday, July 31, 2012 6:30 AM
  • Imports System.Collections.Generic
        Public Sub SplitTest()
            Dim StringToCheck As String = "R:\data\R:\cddata\MS\OWDOCASD\AdministratorsoallallR:\cddata\MS\owasdasc\awdasdasdR:\cddata"
            Dim StringToFind As String = "R:\"
            Dim Start As Integer = 1
            Dim Found As Integer = 0
            Dim Occurance As New List(Of String)
            While (Found >= 0)
                Found = StringToCheck.IndexOf(StringToFind, Start)
                If (Found > 0) Then
                    Occurance.Add(StringToCheck.Substring(Start - 1, Found - Start + 1))
                    Start = Found + 1
                End If
            End While
            Occurance.Add(StringToCheck.Substring(Start - 1, StringToCheck.Length - Start + 1))
            For Each s As String In Occurance
                Console.WriteLine(s)
            Next
            Console.ReadKey()
        End Sub

    Sample output:

    R:\data\
    R:\cddata\MS\OWDOCASD\Administratorsoallall
    R:\cddata\MS\owasdasc\awdasdasd
    R:\cddata


    Tuesday, July 31, 2012 7:53 AM
    Answerer