Results 1 to 7 of 7

Thread: 3 levels of 'split' string; 1 level not working

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    3 levels of 'split' string; 1 level not working

    The attached code works, apart from the area between the 2 sets of "---------------" lines; specifically I think lines labelled 10 and 20.

    The highlighted area of code splits the text using " " as delimiter, then splits any remaining hyphenated words by "-" as delimiter, but retains the hyphen, on one or other of the resulting 'words'.

    The problem is that although it displays the correct words in InkEditBox 2, it overwrites the value of articleSpltArr(j) with the value of hyphenWordArr(1), and loses the original string value at that value of j. Not what I had in mind.

    I had thought that redimming the array as I have done would preserve the original value, add another string to the array, and make that one = hyphenWordArr(1). But this isnt what's happening.

    hyphenWord and hyphenWordArr only exist to help performing the split; they aren't needed for anything else.

    Short of putting hyphenWordArr(1) and articleSpltArr(j)'s values into a third array, what can I do to make this work properly?

    Attached code and test file, and rtf of form1.frm

    Thanks
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: 3 levels of 'split' string; 1 level not working

    You really should have posted the snippet of code where you are having trouble.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: 3 levels of 'split' string; 1 level not working

    Ok, no worries, thought maybe it was better to put the whole thing up and then highlight the bit in question...

    If InStr(articleSpltArr(j), "-") Then
    hyphenWord = articleSpltArr(j)
    hyphenWordArr() = Split(hyphenWord, "-")
    articleSpltArr(j) = hyphenWordArr(0) & "-"
    InkEdit2.Text = InkEdit2.Text & articleSpltArr(j)
    10 ReDim Preserve articleSpltArr(0 To UBound(articleSpltArr) + 1) As String
    20 articleSpltArr(j) = hyphenWordArr(1)
    End If

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: 3 levels of 'split' string; 1 level not working

    I'm not a VB6 guy but when I read what it is that you're trying to do, my first thought is to just use RegEx. If I have this correctly: you're wanting to get all words separated by whitespace and by hyphens, discard the whitespace but preserve the hyphen. If so then you can use the following pattern on a RegEx split:
    Code:
    (-)|\s
    The grouping of the hyphen will preserve the character in the split while leaving the whitespace ungrouped discards it. So if you were to have the following text:
    Code:
    this is some words and this-word was hypenated just like this-one too
    Then it would be split like this:
    Code:
    this
    is
    some
    words
    and
    this
    -
    word
    was
    hypenated
    just
    like
    this
    -
    one
    too
    Now I've read that VB6 doesn't come with native RegEx support but you are able to use Microsoft's VBScript RegEx library(look here). Also for what it's worth, here is an example in .NET:
    Code:
    Dim text As String = Console.ReadLine() 'Get some text
    Dim words() As String = Regex.Split(text, "(-)|\s") 'Split the text by the hyphen and whitespace while preserving the hyphen
    Console.WriteLine(String.Join(Environment.NewLine, words)) 'Print out the split up text
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: 3 levels of 'split' string; 1 level not working

    That looks like a very useful level of control to have. Maybe I should upgrade to .NET; I only use VB6 because its what I've had on the pc for a few years now.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: 3 levels of 'split' string; 1 level not working

    I'm not going to tell you whether you should move to .NET or not because from what I understand there is a learning curve. But you are able to use RegEx within VB6 using the VBScript library, I would first explore trying that and if you get stumped then come back here with what you're having trouble with.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: 3 levels of 'split' string; 1 level not working

    Quote Originally Posted by moorea21 View Post
    That looks like a very useful level of control to have.
    A VB6-version could look like:

    Code:
    Private Sub Form_Load()
      EnumWordsOn "...foo, bar...? foo-bar, foo- bar, foo -bar."
    End Sub
    
    Sub EnumWordsOn(S, Optional Match)
      With CreateObject("VBScript.RegExp")
        .Global = 1: .Pattern = "\w+|-"
        For Each Match In .Execute(S): Debug.Print Match: Next
      End With
    End Sub
    The above printing out:
    Code:
    foo
    bar
    foo
    -
    bar
    foo
    -
    bar
    foo
    -
    bar
    Olaf

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width