-
Dec 31st, 2014, 03:32 AM
#1
Thread Starter
Addicted Member
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.
-
Dec 31st, 2014, 08:42 AM
#2
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
-
Dec 31st, 2014, 08:58 AM
#3
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.
-
Dec 31st, 2014, 09:47 AM
#4
Re: Two Questions about Lines in an Array
Originally Posted by jmcilhinney
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.
-
Dec 31st, 2014, 10:22 AM
#5
Re: Two Questions about Lines in an Array
Originally Posted by Edgemeal
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)
-
Dec 31st, 2014, 02:53 PM
#6
Thread Starter
Addicted Member
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..
-
Dec 31st, 2014, 07:37 PM
#7
Re: Two Questions about Lines in an Array
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 31st, 2014, 10:22 PM
#8
Re: Two Questions about Lines in an Array
Originally Posted by digitaldrew
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?
-
Dec 31st, 2014, 10:35 PM
#9
Re: Two Questions about Lines in an Array
@JM...
mixing VB and C#???
been on the sherry?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 1st, 2015, 12:29 AM
#10
Re: Two Questions about Lines in an Array
Originally Posted by .paul.
@JM...
mixing VB and C#???
been on the sherry?
You might think. Maybe that Christmas pudding was stronger than advertised.
-
Jan 1st, 2015, 01:32 AM
#11
Re: Two Questions about Lines in an Array
Originally Posted by jmcilhinney
You might think. Maybe that Christmas pudding was stronger than advertised.
haha
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|