1

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
DaveTheMinion
  • 664
  • 3
  • 22
  • 45
  • Are you certain that there is delimiter? It is quite possible that the text is just one continuous line. – Fionnuala Nov 30 '14 at 23:22
  • @Fionnuala One of things the program does is read the text from the database back into a rich textbox. Upon doing this, all of the lines that were made when the data was written to the database is kept when it is read from the database back into the rich textbox. Because of this, I know for a fact that it is not being interpreted as one continuous line. – DaveTheMinion Nov 30 '14 at 23:28
  • [Change your Split() to look more like this answer.](http://stackoverflow.com/a/1547483/656243) – Lynn Crumbling Nov 30 '14 at 23:44
  • @LynnCrumbling Please see my edit. Is that what you are talking about? If so, it isn't working. – DaveTheMinion Nov 30 '14 at 23:58
  • I would have gone with `Split(New String() { Environment.NewLine }, StringSplitOptions.None)`. – Lynn Crumbling Dec 01 '14 at 00:00
  • @LynnCrumbling That's not working either. – DaveTheMinion Dec 01 '14 at 00:06
  • You need to take a look at your string and find out what ASCII character is delimiting the lines. Examine the values, character by character, under a debugger. It isn't separated using conventional `\n`s. Find out what's appearing after the first line, and let us know. – Lynn Crumbling Dec 01 '14 at 00:08
  • You may want to post the code that is *writing* the database value, or a hex dump of the all bytes in the string. – Lynn Crumbling Dec 01 '14 at 00:09
  • @LynnCrumbling I didn't change anything as far as I can tell, but somehow, the code that I posted in my edit is working now. I can't figure it out. Now I need to figure out how to remove trailing and leading blank lines. – DaveTheMinion Dec 01 '14 at 00:14
  • Since you've got the lines as an Array, I'd convert it to a List, and delete the list elements that you don't want (you'd can't modify an Array). – Lynn Crumbling Dec 01 '14 at 00:17
  • @LynnCrumbling I'd actually like to strip the leading and trailing tags before splitting the string into an array. – DaveTheMinion Dec 01 '14 at 00:19
  • Cool; sounds like a separate question. – Lynn Crumbling Dec 01 '14 at 00:30
  • @LynnCrumbling I'll ask it if necessary, but I believe that I found a solution to the problem. By the way, I was able to find that I could split the string with `vbLf` by removing split delimiters from the array one at a time. – DaveTheMinion Dec 01 '14 at 00:31
  • 1
    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. – Chris Dunaway Dec 01 '14 at 16:29

1 Answers1

1

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.

DaveTheMinion
  • 664
  • 3
  • 22
  • 45