Hi could someone please give me an example of how to read through a text file to the end and read every 5th line as a string? I know how to read a specific line in the text file using
line = System.IO.file.readAllLines(filepath)(linenum)
and also using streamreader to read each line etc..
But I want to go through the whole text file and pick out every certain number of lines. Pretty sure it's got something to do with a loop but I'm not too clued up yet.
As files are not line based, you would need to read all the lines and pick out the ones that you want.
You can use the Where method with the overload that gives you the index of the item, and the Mod operator to determine where every fifth line is:
Dim lines As String() = _
System.IO.File.ReadLines(filepath).Where(Function(line, i) i Mod 5 = 0).ToArray()
The number that you compare the expression to determines which lines you get. i Mod 5 = 0 starts at the first line and then every fifth from there, while i Mod 5 = 4 starts at the fifth line and then every fifth from there.
(The ReadLines method is better than the ReadAllLines for this, as it doesn't read all the lines into memory first, but returns an enumerator so that you can process the lines as they are read.)
I have a very large set of data that represents cartesian coordinates in the form x0,y0,z0,x1,y1,z1...xn,yn,zn. I need to create a new line at the end of each xyz coordinate. I have been trying to record a macro that moves a certain number of spaces from the beginning of each line, then creates a new line. This, of course, will not work since the number of digits in each xyz coordinate differs.
How can I create a macro to do this in Microsoft Word?
Try this:
Public Sub test()
Dim s As String
Dim v As Variant
Dim t As String
Dim I As Long
s = "x0,y0,z0,x1,y1,z1,xn,yn,zn"
v = Split(s, ",")
t = ""
For I = LBound(v) To UBound(v)
t = t + v(I)
If I Mod 3 = 2 Then
t = t + vbCr
Else
t = t + ","
End If
Next I
t = Left(t, Len(t) - 1)
Debug.Print t
End Sub
The Split function splits a string along the delimiter you specify (comma in your case), returning the results in a 0-based array. Then in the For loop we stitch the pieces back together, using a carriage return (vbCR) every third element and a comma otherwise.
The final (optional) step is to remove the trailing carriage return.
Hope that helps
The question placed before us was most clearly asked
“Please produce a macro sufficient to the task
I have Cartesian coordinates, a single line of these
Array them in many lines, triplets if you please!”
Instinctively we start to code, a solution for this quest
Often without asking, “Is this way truly best?”
But then another scheme arises from the mind
That most venerated duo: Word Replace and Find
Provide the two textboxes each an encantation
Check the Wildcard option and prepare for Amazation!
Forgive me!
In Word open Find/Replace
Click the More button and check the Use wildcards box
For Find what enter ([!,]{1,},[!,]{1,},[!,]{1,}),
For Replace with enter \1^p
Use Find Next, Replace and Replace All as usual
How it works
With wildcards, [!,]{1,} finds one or more chars that are NOT commas. This idiom is repeated 3 times with 2 commas separating the 3 instances. This will match 3 comma-delimited coordinates. The whole expression is then wrapped in parentheses to created an auto-numbered group (in this case Group #1). Creating a group allows us to save text that matches the pattern and use it in the Replace box. Outside of the parentheses is one more comma, which separates one triplet of coordinates from the next.
In the Replace box \1 retrieves auto-numbered group 1, which is our coordinate triplet. Following that is ^p which is a new paragraph in Word.
Hope that helps!
In VB.net, I want to make a counting program using a Function and a Sub.
There is a textbox to input a date and a button to exercise the programme in Form1.
I have a txt file which was extracted from MS-Excel with sequential date of time at its column A.
And from that txt file, I want to count the number of date(Actually string) such as "18-Jun-12".
The answer showing the count should be in the format of msgbox in the Sub.
I really have no idea how to link a Function and a Sub using variable, because I am just beginner.
Any help will be gratefully accepted.
If the fields are delimited by comma you must be careful since the field itself could contain a comma. Then you cannot differentiate between the value and the delimiter. You either could enclose the fields with quotes to mask them. But then you should use an available CSV parser anyway.
If the values never contain comma and you want a simple solution use File.ReadLines or File.ReadAllLines to read the lines and String.Split to get all fields per line.
Here's a simple approach using a little bit of LINQ to count all lines which contain the searched date (as string):
Dim linesWithThatDate = From line in File.ReadLines("Path to File")
Where line.Split(","c)(0).Trim() = "18-Jun-12"
Dim count = linesWithThatDate.Count()
As an aside, if the user must enter a date you could use a DateTimePicker control instead. Then you should also use Date.Parse(line.Split(","c)(0).Trim()) or Date.TryParse to get a real date.
How can I append text in a rich text box? Say, for example, that I need to append it on line 40 at column 30.
Take your string, count the lines, then use string manipulation to manually insert your string in the correct position.
You could count lines by counting occurrences of '\n' and for column you just add column number to the position that you find your line at.
If this is too little info, ask me and I can paste you a c# code for it.
I'm new to vb.net. I would like to know how i could i/p a string via textbox and display its individual characters into a datagrid view in diff rows?
Are you only using a single column in the datagridview? if so you could do this easily by stepping through each character in the string and adding a row containing that character... see the code example below
InputString = TextBox1.Text
For a = 0 To InputString.Length - 1
Datagridview1.Rows.Add(InputString(a))
Next
Cant see why you'd want to do this though - If this isn't what you're trying to accomplish please give us more details