Splitting String by New Line VB.NET - vb.net

I'm writing a program that needs to split text in a RichTextBox by a new line. I've tried
For Each Line As String In RichTextBox1.Text.Split(vbNewLine)
And I've tried
For Each Line As String In RichTextBox1.Text.Split(System.Enviroment.NewLine)
Neither are working. It works if there's only one line of text but not after that. Any suggestions?

I guess the easiest way to do it is to use RichTextBox.Lines like so
For Each Line As String In RichTextBox1.Lines
' Do whatever
Next
Not sure why splitting by Environment.NewLine didn't work for you though, it worked fine for me when I just tested.
Edit: Just noticed the comment on the question, oops. That'll teach me for being slow with posting.

Related

Determine Number of Lines in a String Read in from an Access Database

I am writing a program in Visual Basic that writes and reads to and from a Microsoft Access Database. When reading from the database, one of the functions that I am trying to perform is to determine the number of lines in a multi-line string that was written to the database and then subsequently read from the database. Here's what I have tried so far with no luck.
Dim stringLines() As String = databaseReader("multilineString").ToString.Split(CChar("Environment.NewLine"))
Dim stringLinesCount As Integer = stringLines.Length
For some reason, this always results in stringLinesCount being equal to one, regardless of how many lines the string has. In this example, I am using Environment.NewLine, but I have tried \n, \r, vbCr, vbLf, and vbCrLf as well, and they all result in a value of one. Since none of these seem to be working, what can I use instead to determine the number of lines?
Edit:
Dim splitCharacters() As Char = {CChar(vbCrLf), CChar(vbCr), CChar(vbLf), CChar(Environment.NewLine), CChar("\n"), CChar("\r")}
Dim stringLines() As String = databaseReader("multilineString").ToString.Split(splitCharacters)
Dim stringLinesCount As Integer = stringLines.Length
Since Chris Dunaway provided the answer that I view as helpful but posted it as a comment, here's what he said:
VB cannot use C# style escape sequences, so CChar("\n") and CChar("\r") is meaningless in VB. Also, calling CChar("Environment.NewLine") is wrong because you are trying to convert the actual string "Environment.NewLine" to a single character, which obviously won't work. You can just use Environment.Newline directly in the call to String.Split.
If Chris decides to post his comment as an answer, please let me know so that I may remove this.

VB.net generate a variable for each line in a .txt (nr. of lines unknown)

I'm trying to generate a variable for each line in a .txt file. It works for the first line but not the ones below. Would be awesome if you guys could help me proceed! This is what I have so far. I was thinking of using EOF but I couldn't get it to work -.-
Dim sr As New StreamReader(cleanfile)
Dim coins As String() = sr.ReadLine.Split(Environment.NewLine)
The number of variables must be known at compile-time. However, since the number of lines in the file can vary, you don't know this number at compile-time.
Thus, a separate variable for each line is not the solution for your problem.
Your solution is to use an array. To create one, you can simply use File.ReadAllLines:
Dim lines As String() = File.ReadAllLines(cleanfile)
Then, you can access the lines as lines(0), lines(1) or iterate through them using the For Each statement:
For Each line in lines
' Do something with line
' ...
Next

Using streamreader I can read the next line of words, but can I read the previous one?

Can I read the previous line using StreamReader?
Dim previousfile As New StreamReader("file.txt")
If previousfile.Peek <> +1 Then
txtName.text = previousfile.ReadLine
End If
Can anyone help?
you cannot read the previous line - StreamReader really is a forward only type of reader. when you read a line... thats it. you cannot go back.
why dont you hold the previous line being read in a temp variable or maybe use the FileStream which has a Seek method which maybe of some use to you?
or why not read the entire contents into a collection of strings and splitting it on some delimeter for example?
You can't read backwards with a StreamReader, but if you read all of the lines in first you can traverse them however you like. This does mean reading the whole file in up front, which may be less efficient depending on your usage, but this method would do the job and give you an array:
var lines = File.ReadAllLines("file.txt")

How to replace text to a 'vbcr'

Using vb.net and visual studio 2012.
I have a bunch of strings using custom text replacement.
By that I mean that they are all one-line strings using, for example, "&1" to replace 'vbcr' and so on.
I have to take this string and replace all the "&1" by a vbcr.
I tried using regex and stringbuilder replace. Here is an example:
finaltext = firsttext.Replace("&1", vbcr)
But doing it this way results in replacing the "&1" by a simple space.
I thought that vbcr was the problem but I tried to reverse my code by:
finaltext = firsttext.Replace(vbcr, "&1")
The vbcr were correctly replaced by "&1" so I don't understand why my original code is not working.
I know it's possible using a long complicated custom function but I would prefer to avoid this solution if possible.
According to MSDN, the syntax of String.Replace states that the first argument is the oldValue and the second argument is the one that replaces it.
Also, if you need newlines, you should be using Environment.NewLine:
finaltext = firsttext.Replace("&1", Environment.NewLine)
Environment.NewLine is easier to read and it also takes care of the platform for you, being
A string containing "\r\n" for non-Unix platforms, or a string
containing "\n" for Unix platforms.

VB seems to lose newlines when called over COM

I have a VB method
Public Sub append_text(ByVal s As String)
f1.TextBox1.AppendText(s)
End Sub
which is called over COM from C++
_bstr_t b(L"test\nnew\nlines\n");
ATLENSURE_SUCCEEDED(t->append_text(b));
But the text box ends up saying
testnewlines
Without the aforementioned new lines.
Why is that then?
For the sake of completeness, posting my comment as an answer (now that I know it's correct...):
Different operating systems consider different character combinations as new lines. The *nixes, for instance, use a single \n, as in your code. Windows, on the other hand, uses the \r\n combination. Therefore, the single \n in your string just isn't enough to be considered a new line marker. Using \r\n will do the trick.
Eran is right.
To fix it on the VB side, try this
Dim s2 As String = s.Replace(vbLf, vbCrLf)
f1.TextBox1.AppendText(s2)
EDIT Sideshow Bob has compiled and tested this.