How to use Split to tokenize text file using the new line character as a delimiter - vba

Using VBA how would one go about using the VBA.Split method to tokenize text file using the new line character as a delimiter?
The following don't seem to work:
Split(myText, "\n")
Split(myText, vbCrLf)
Split(myText, vbNewLine)

After a little investigation turns out the answer is:
Split(data, vbLf)

If you are working within VBA, you don't need all the VBA tags:
Split(data, vbLf)

Another way to get it work is
split(myText,Chr(13) + Chr(10))

Found the anser on chandoo:
Split(ActiveCell.Value, Chr(10))

Related

Replacing "\" in openrefine

it seems to me that the expression
value.replace("\", "")
doesn't work. It doesn't also if there are other characters with \, like:
value.replace("\xnes", "")
I've got some data with those and I need to erase it. Why is it behaving like this?
Thanks

Indentify line breaks in excel VBA

I'm trying to identify a cell has line breaks(not the menu bar cell option, actual multiple lines through alt+enter), and extract each line separatedely
I have tried both
InStr(str, "\n") < 0
and
Split(str, "\n")
But doesn't seem to be working
VBA is not C# ("\n"). Line breaks you'll find on: vbCr or vbLf or vbCrLf constants.
For further information, please see:
vbCr
vbLf
vbCrLf
[EDIT]
Points to Mat's Mug answer! I forgot about vbNewLine constant.
There are no escape sequences in VBA. Use the built-in vbNewLine constant instead for the equivalent:
hasLineBreaks = InStr(str, vbNewLine) > 0
Per MSDN, vbNewline returns a Platform-specific new line character; whichever is appropriate for current platform, that is:
Chr(13) + Chr(10) [on Windows] or, on the Macintosh, Chr(13)
So you don't need to work with ASCII character codes, or even with their respective built-in constants.
Except Excel will strip CR chars from cell and shape contents, and this has nothing to do with VBA (the CR chars would be stripped all the same and "\n" wouldn't work for correctly reading that Excel data in C#, Javascript, or Python either) and everything to do with the context of where the string came from.
To read "line breaks" in a string with the CR chars stripped, you need to look for line feed chars in the string (vbLf).
But if you systematically treat the line feed character as a line ending, you'll eventually run into problems (esp.cross-platform), because ASCII 10 all by itself isn't an actual line break on either platform, and you'll find ASCII 13 characters in strings you thought you had stripped line breaks from, and they'll still properly line-break on a Mac, but not on Windows.
Consider either:
Split(str, Chr(10))
or
Split(str, Chr(13))
You may need to try both if the data has been imported from external source.

Replace CarriageReturn Using Replace in VBA

I am relatively new to VBA and want to accomplish something pretty simple but am confused as to why this is not working. I am reading in lines from a file and if opened in notepad++, each line has a CRLF at the end. I would like to just remove the CR. In notepad++ I can do a replaceall, replacing CRLF with LF and things work great. However, the test I have in VBA right now is not doing this correctly. Below is an example of a string I'm dealing with in Notepad++:
Summary - I went for a walk in the parkCRLF
I want the string to become, Summary - I went for a walk in the parkLF
I am writing out to a file as a test in order to see if its working. Below is my code:
Do While Not txtStream.AtEndOfStream
str = txtStream.ReadLine
edited = Replace(str, Chr(13) & Chr(10), Chr(10))
stream.WriteLine (edited)
Loop
txtStream.Close
The code is being executed without error but the CRLF is still at the end of each line in the newly written file...Maybe I'm missing something obviously but the replace does not seem to be picking up on what I'm searching for. Any help or advice on this would be greatly appreciated!
Even figuring out if the end of the line ends with a CRLF would be a step in the right direction at this point.
Thank you.
WriteLine automatically places an end of line after your string
You should replace it with #Mark's suggestion: stream.Write(edited)
This performs better:
stream.Write(Replace(txtStream.ReadAll, vbCrLf, vbLf))
or
stream.Write(Replace(txtStream.ReadAll, vbCr, vbLf))
.
Details about ReadAll
You are reading a line so it will remove the new line characters. Your replace statement has nothing to do. You are then writing a line which will add new line characters.
Also, you aren't closing the output stream in the code sample you provided.
You will want something like this:
Do While Not txtStream.AtEndOfStream
str = txtStream.ReadLine
stream.Write str & Chr(10)
Loop
txtStream.Close
stream.Close

FileSystem.WriteAllText adds non-printable characters

Here are two methods for writing text to a file in VB.Net 2012. The first one prepends the same three non-printable characters to each file: . The second one works as expected and does not add the three characters. objDataReader is an OleDB datareader.
Any idea why?
Greg
My.Computer.FileSystem.WriteAllText(lblLocation.Text & "\" &
objDataReader("MessageControlId").ToString & ".txt", objDataReader("MsgContents").ToString, False)
Using outfile As New StreamWriter(lblLocation.Text & "\" & objDataReader("MessageControlId").ToString & ".txt")
outfile.Write(objDataReader("MsgContents").ToString)
End Using
Thanks. I found the entry below I after Googled BOM, in case anyone wants a more detailed explanation. While the BOM was not visible in a text editor it did cause problems when I passed the file to our HL7 interface engine.
Greg
Write text files without Byte Order Mark (BOM)?

New line character in VB.Net?

I am trying to print a message on a web page in vb.net. I am trying to get the messages in new lines. I tried using the "\r\n" and the new line character. But this is getting printed in the page instead of it comming to the next line. Please let me know if there is any alternative.
Check out Environment.NewLine. As for web pages, break lines with <br> or <p></p> tags.
Environment.NewLine is the most ".NET" way of getting the character, it will also emit a carriage return and line feed on Windows and just a carriage return in Unix if this is a concern for you.
However, you can also use the VB6 style vbCrLf or vbCr, giving a carriage return and line feed or just a carriage return respectively.
The proper way to do this in VB is to use on of the VB constants for newlines. The main three are
vbCrLf = "\r\n"
vbCr = "\r"
vbLf = "\n"
VB by default doesn't allow for any character escape codes in strings which is different than languages like C# and C++ which do. One of the reasons for doing this is ease of use when dealing with file paths.
C++ file path string: "c:\\foo\\bar.txt"
VB file path string: "c:\foo\bar.txt"
C# file path string: C++ way or #"c:\foo\bar.txt"
You need to use HTML on a web page to get line breaks. For example "<br/>" will give you a line break.
If you are using something like this.
Response.Write("Hello \r\n")
Response.Write("World \r\n")
and the output is
Hello\r\nWorld\r\n
Then you are basically looking for something like this
Response.Write("Hello <br/>")
Response.Write("World <br/>")
This will output
Hello
World
you can also just define "<br />" as constant and reuse it
eg.
Public Const HtmlNewLine as string ="<br />"
Response.Write("Hello " & HtmlNewLine)
Response.Write("World " & HtmlNewLine)
it's :
vbnewline
for example
Msgbox ("Fst line" & vbnewline & "second line")
Try Environment.NewLine.
Your need to use the html/xhtml break character:
<br />
you can solve that problem in visual basic .net without concatenating your text, you can use this as a return type of your overloaded Tostring:
System.Text.RegularExpressions.Regex.Unescape(String.format("FirstName:{0} \r\n LastName: {1}", "Nordanne", "Isahac"))
In asp.net for giving new line character in string you should use <br> .
For window base application Environment.NewLine will work fine.
VbCr
Try that.
In this case, I can use vbNewLine, vbCrLf or "\r\n".
vbCrLf is a relic of Visual Basic 6 days. Though it works exactly the same as Environment.NewLine, it has only been kept to make the .NET api feel more familiar to VB6 developers switching.
You can call the String.Replace() function to avoid concatenation of many single string values.
MsgBox ("first line \n second line.".Replace("\n", Environment.NewLine))
Environment.NewLine or vbCrLf or Constants.vbCrLf
More information about VB.NET new line:
http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx
I had the need to store line breaks in a string in a SQL table and have them displayed in vb.NET. My solution was to include a string like this in my database:
"This is the first line{0}This is the second{0}This is the third"
In vb.NET, I processed the string like this before using it:
Label2.Text = String.Format(stringFromSQLquery, vbCrLf)
This replaces every occurance of {0} with vbCrLf