Results 1 to 22 of 22

Thread: Splitting string and adding contents into listview columns.

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    32

    Wink Splitting string and adding contents into listview columns.

    Hello everyone,

    I'm looking at splitting a string which is generated and then inserting the values into a listview.

    The string that I'm trying to split will look something like this;
    Name:  2WOey.jpg
Views: 1700
Size:  16.7 KB

    The listview I want to input the values in looks like this;
    Name:  2WOeR.png
Views: 1936
Size:  23.3 KB

    The string is made up of 3 values which is grabbed from a php page which grabs all usernames (and their equivalent emails and ranks) from a MySQL db.
    I've currently got it working slightly... and by that I mean it will split the string and insert them into ONLY the first column like above, the full code for this so far is:
    Code:
     Sub getUsers()
    
            Dim Str As Stream
            Dim srRead As StreamReader
    
            Dim req As WebRequest = WebRequest.Create(url)
            Dim resp As WebResponse = req.GetResponse
            Str = resp.GetResponseStream
            srRead = New StreamReader(Str)
    
            Dim readStr As String = srRead.ReadToEnd
    
            readStr = readStr.Replace("</table>", "")
            readStr = readStr.Replace("<table>", "")
            readStr = readStr.Replace("<tr>", "")
            readStr = readStr.Replace("<td>", "")
            readStr = readStr.Replace("</tr>", "")
            readStr = readStr.Replace("</td>", "#")
    
            MsgBox(readStr)
    
            Dim splitStr() As String = readStr.Split("#")
            Dim split2 As String
    
            For Each split2 In splitStr
    
                If split2 = "" Then
                Else
                    userList.Items.Add(split2)
                End If
    
            Next
        End Sub
    Can anyone help me out?
    Thanks in advanced.

  2. #2
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    You need to use the For loop with a Step of 3 and grab indexes i, i + 1, and i + 2 as you go along. The ListViewItem constructor has an overload that takes a string array, use that to define the full ListViewItem text in each of the 3 columns.

    Something like this:
    Code:
    Dim len As Integer = 3
    For i As Integer = 0 To SplitArrayLength - len Step len
    	ListView1.Items.Add(New ListViewItem(New String() {SplitArray(i), SplitArray(i + 1), SplitArray(i + 2)}))
    Next
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Splitting string and adding contents into listview columns.

    try this:

    Code:
    Sub getUsers()
    
        Dim Str As Stream
        Dim srRead As StreamReader
    
        Dim req As WebRequest = WebRequest.Create(url)
        Dim resp As WebResponse = req.GetResponse
        Str = resp.GetResponseStream
        srRead = New StreamReader(Str)
    
        Dim readStr As String = srRead.ReadToEnd
    
        readStr = readStr.Replace("</table>", "")
        readStr = readStr.Replace("<table>", "")
        readStr = readStr.Replace("<tr>", "")
        readStr = readStr.Replace("<td>", "")
        readStr = readStr.Replace("</tr>", "")
        readStr = readStr.Replace("</td>", "#")
    
        MsgBox(readStr)
    
        Dim splitStr() As String = readStr.Split("#"c)
    
        For x As Integer = 0 To splitStr.GetUpperBound(0) Step 3
            userList.Items.Add(New ListViewItem() splitStr.Skip(x).Take(3).ToArray)
        Next
    
    End Sub
    Last edited by .paul.; May 19th, 2013 at 07:09 PM.

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    I don't see the need to use LINQ though for something simple enough to index yourself and declare the array elements manually. Otherwise you're generating an IEnumerable result calling methods to start from the beginning, skip x elements, and take 3 from that location, then cast to an array. Indexing directly is more efficient.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    32

    Re: Splitting string and adding contents into listview columns.

    Quote Originally Posted by AceInfinity View Post
    You need to use the For loop with a Step of 3 and grab indexes i, i + 1, and i + 2 as you go along. The ListViewItem constructor has an overload that takes a string array, use that to define the full ListViewItem text in each of the 3 columns.

    Something like this:
    Code:
    Dim len As Integer = 3
    For i As Integer = 0 To SplitArrayLength - len Step len
    	ListView1.Items.Add(New ListViewItem(New String() {SplitArray(i), SplitArray(i + 1), SplitArray(i + 2)}))
    Next
    Bit of a late response but that's sorted it.

    I've currently got
    Code:
            Dim splitStr() As String = readStr.Split("#")
            Dim SplitArrayLength As Integer = 0
    
            Dim x As String = "#"
    
            For Each x In readStr
                SplitArrayLength = SplitArrayLength + 1
            Next
    
            Dim Len As Integer = 3
            For i As Integer = 0 To SplitArrayLength - Len Step Len
                userList.Items.Add(New ListViewItem(New String() {splitStr(i), splitStr(i + 1), splitStr(i + 2)}))
            Next
    which seems to do the job!

    There's probably a more efficient way but when I figure that out I'll change it.
    Thanks to both of you.

  6. #6
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    Just as a tip, you should be defining your array variables like this:
    Code:
    Dim splitStr As String() = readStr.Split("#")
    With the parenthesis after the type, not the variable name itself. MSDN coding conventions will point this out to you, it's better practice.
    Last edited by AceInfinity; May 26th, 2013 at 05:15 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Splitting string and adding contents into listview columns.

    Quote Originally Posted by AceInfinity
    Split("#")
    that version of split requires a char parameter:

    Code:
    Split("#"c)
    see MSDN:
    http://msdn.microsoft.com/en-us/libr...ing.split.aspx

    it's correct practice...
    Last edited by .paul.; May 26th, 2013 at 04:35 PM.

  8. #8
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    Quote Originally Posted by .paul. View Post
    that version of split requires a char parameter:

    Code:
    Split("#"c)
    see MSDN:
    http://msdn.microsoft.com/en-us/libr...ing.split.aspx

    it's correct practice...
    I copied his code I didn't write that.

    Quote Originally Posted by ComYOU
    I currently got
    Code:
    Dim splitStr() As String = readStr.Split("#")
    Dim SplitArrayLength As Integer = 0
    
    Dim x As String = "#"
    
    For Each x In readStr
    	SplitArrayLength = SplitArrayLength + 1
    Next
    
    Dim Len As Integer = 3
    For i As Integer = 0 To SplitArrayLength - Len Step Len
    	userList.Items.Add(New ListViewItem(New String() {splitStr(i), splitStr(i + 1), splitStr(i + 2)}))
    Next
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Splitting string and adding contents into listview columns.

    Quote Originally Posted by AceInfinity View Post
    I copied his code I didn't write that.
    i'd still like to see the MSDN article that says array parentheses should be placed after the type

  10. #10
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    Alright, as you requested: http://msdn.microsoft.com/en-us/libr.../h63fsef3.aspx

    I did not mean any offense. This is coding conventions though directly from MSDN.

    Quote Originally Posted by Arrays
    Put the array designator on the type, not on the variable. For example, use the following syntax:

    Code:
    Dim letters4 As String() = {"a", "b", "c"}
    Last edited by AceInfinity; May 26th, 2013 at 05:06 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Splitting string and adding contents into listview columns.

    There is no practical reason to follow that misguided convention.
    Both parentheses on the array name + parentheses on the type work equally well.

  12. #12
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    Quote Originally Posted by .paul. View Post
    There is no practical reason to follow that misguided convention.
    Both parentheses on the array name + parentheses on the type work equally well.
    Why no practical reason? They mention things for reasons. Perhaps they are deciding to remove support for using () after the variable name in future standards of VB? The standard before (VB 2005 and I think VB 2008) mentioned () after the variable name I was told. It has changed, and there must be a reason. Yes, old VB6 methods work still too, but that's not a reason in itself to be using them in VB.net in my opinion.

    In C# for instance we don't put [] after the variable name unless we are indexing.

    Cheers,
    ~Ace
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  13. #13
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Splitting string and adding contents into listview columns.

    This is coding conventions though directly from MSDN.
    Uh-uh! And what do they know?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  14. #14
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    I'm not going to be starting a war here though... I just thought it was worth mentioning. That's all...
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  15. #15
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Splitting string and adding contents into listview columns.

    Perhaps they are deciding to remove support for using () after the variable name in future standards of VB?
    In which case they'll have to remove support for sized arrays too! I'm with paul in thinking this just a bit of silly thing to even think about.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  16. #16
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    It was an example, not my main reason for why it is good/bad as a coding convention. But I do not see MSDN changing their standard convention guidelines (since 2005/2008) without reason like this; a reason most likely exists; unknown at this point. They provide that information "as-is," and that's just the way they say it should be.

    In addition to that, I think I should apologize to .paul. here for putting him on the spot like that. It was not my initial intention, and I didn't even know or see that his code reflected that same code I was pointing out in OP's code.

    In which case they'll have to remove support for sized arrays too!
    No... For instance:
    vbnet Code:
    1. Dim arr As String() = New String(5) {}
    vbnet Code:
    1. Dim x As Integer() = New Integer(4) {}
    Last edited by AceInfinity; May 26th, 2013 at 05:24 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Splitting string and adding contents into listview columns.

    I just found it strange that you picked up on a matter of coding style + completely ignored an implicit conversion error.

  18. #18
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    Quote Originally Posted by .paul. View Post
    I just found it strange that you picked up on a matter of coding style + completely ignored an implicit conversion error.
    It was left to right reading instinct . I seen () before I missed seeing a c there. But yes, admittedly I didn't see it until you mentioned it. I was more focused on the loop OP was using to reflect indexing of that array instead also.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  19. #19
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Splitting string and adding contents into listview columns.

    Well, that's even sillier!!!!!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  20. #20
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    Quote Originally Posted by dunfiddlin View Post
    Well, that's even sillier!!!!!
    Similar to the way you'd write it in C#:
    c# Code:
    1. int[] x = new int[5];

    Just a character less (; is added in C# lol).

    When you omit the {} in VB.net though, it thinks you are using the constructor. Granted, that page on MSDN is very odd and unclear at explaining the good conventions for fixed size declarations, which is not good at all.

    For example, they say NOT to do this:
    Code:
    Dim letters6(2) As String
    letters6(0) = "a"
    letters6(1) = "b"
    letters6(2) = "c"
    With no alternative.

    They also say this:
    Do not use the following syntax:
    Code:
    Dim letters5() As String = {"a", "b", "c"}
    Then proceed to say:
    Use the { } syntax when you declare and initialize arrays of basic data types. For example, use the following syntax:
    Code:
    Dim letters5() As String = {"a", "b", "c"}
    Notice that these are the same code...
    Last edited by AceInfinity; May 26th, 2013 at 05:35 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Splitting string and adding contents into listview columns.

    my guess is that even the mighty Microsoft are not infallible.
    forums like this are invaluable for identifying + pointing out their errors

  22. #22
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Splitting string and adding contents into listview columns.

    Quote Originally Posted by .paul. View Post
    my guess is that even the mighty Microsoft are not infallible.
    forums like this are invaluable for identifying + pointing out their errors
    Nobody is perfect *thumbs up* I agree.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

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