Results 1 to 11 of 11

Thread: Two Questions about Lines in an Array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    158

    Two Questions about Lines in an Array

    Hey everyone..

    I currently have a textbox, and that textbox has 100 items in it.. I want to pull the items 10 at a time so I can process them at once. Here is the code I am currently using:
    Code:
    Dim page = Me.txtDomains.Lines.Cast(Of String).Skip(n * NumericUpDown3.Value).Take(NumericUpDown3.Value).ToArray()
    Question 1: How would it be possible to use the above code, but make sure any blank/empty lines are not added?

    thank you in advance!

    Edit - removed question 2. I was able to remove the last character of the string using:
    Code:
    s = s.Remove(s.Length - 1)

    Final Edit - It looks like I was able to solve both my problems by changing the way I formatted the string:
    Code:
    Dim s As String = String.Join(",", page.Where(Function(x) Not String.IsNullOrEmpty(x)))
    Last edited by digitaldrew; Dec 31st, 2014 at 04:20 AM.

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Two Questions about Lines in an Array

    1. You could use the Linq Where function on the result of Take. You pass in a lambda function to define which element you want included which, in your case, will be everything where the element isn't an empty string.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Two Questions about Lines in an Array

    Firstly, your Cast call is pointless because the Lines property is type String(), so each item is already typed as String without that call.

    As for the question, what you're asking for is to filter out blank lines and filtering is done with the Where method. You would filter out empty lines like this:
    Code:
    .Where(Function(s) s != String.Empty)
    By the way, you should be aware that the Lines property creates a new array every time it's accessed so if you are using that first code snippet in a loop then you should assign the Lines property to a variable first and then use that variable inside the loop. That way, you'll only be creating an array of lines from the TextBox once.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Two Questions about Lines in an Array

    Quote Originally Posted by jmcilhinney View Post
    You would filter out empty lines like this:
    Code:
    .Where(Function(s) s != String.Empty)
    What is != ? (Typo ?, New for 2015 ? )
    Shown as error in VS2010.
    Thanks.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Two Questions about Lines in an Array

    Quote Originally Posted by Edgemeal View Post
    What is != ? (Typo ?, New for 2015 ? )
    Shown as error in VS2010.
    Thanks.
    Oops! Mixing up my C# and VB there. Should be:
    Code:
    .Where(Function(s) s <> String.Empty)

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    158

    Re: Two Questions about Lines in an Array

    Thanks for your replies and help everyone! Quick question... What if I was trying to add some text into the string as well?

    For example:
    Code:
    Dim s As String = String.Concat(Enumerable.Range(0, page.Count).Select(Function(x) "&name" & x.ToString & "=" & page(x)).ToArray)
    Would I just add the .Where to the end, or before the .Select? The code above basically does the same thing and still add empty lines to the string..

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

    Re: Two Questions about Lines in an Array

    Where before Select

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Two Questions about Lines in an Array

    Quote Originally Posted by digitaldrew View Post
    Thanks for your replies and help everyone! Quick question... What if I was trying to add some text into the string as well?

    For example:
    Code:
    Dim s As String = String.Concat(Enumerable.Range(0, page.Count).Select(Function(x) "&name" & x.ToString & "=" & page(x)).ToArray)
    Would I just add the .Where to the end, or before the .Select? The code above basically does the same thing and still add empty lines to the string..
    What if you have 1000 items and your filter excludes 950 of them? What would make more sense: adding something to all 1000 of them and then filtering or filtering first and then adding something to only the 50 that match? You don't need any programming experience to answer that question so how about you put some thought into it first?

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

    Re: Two Questions about Lines in an Array

    @JM...

    mixing VB and C#???
    been on the sherry?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Two Questions about Lines in an Array

    Quote Originally Posted by .paul. View Post
    @JM...

    mixing VB and C#???
    been on the sherry?
    You might think. Maybe that Christmas pudding was stronger than advertised.

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

    Re: Two Questions about Lines in an Array

    Quote Originally Posted by jmcilhinney View Post
    You might think. Maybe that Christmas pudding was stronger than advertised.
    haha

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