How to replace text to a 'vbcr' - vb.net

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.

Related

name.split based on multiple delimiters that have some of the same characters

I have files coming into SFTP that are in the following format: 12345678_STLREPT. On occasion, they have two underscores instead of one. ie: 12345678__STLREPT. This throws my sorting program through a loop. So I attempted to do the top line of code below first, it threw an I/O exception. The second line I can make work if I add "__" as a delimiter, but my question is how does Name.Split work. I haven't been able to determine through my google searches if it stops at the first delimiter it finds in the string.
Basically, If it goes through the list of delimiters it would ideally match one of them, perform the split, then stop before performing the rest.
Example:
Say I have a file, named 1234__ASDF.PDF
Would the following code split it once for the first delimiter (__) then two more times for the second (_)? I feel like it would.
fileInfo.Name.Split(New Char() {"__", "_", "-"})
Is anyone aware of a better solution to this problem?
Thank you,
Code, again. Top throws error, bottom is existing code:
fileInfo = fileInfo.Name.Replace("__", "_")
Dim a() As String = fileInfo.Name.Split(New Char() {"_", "-"})
First, be aware of your data types and what you are stuffing into them. A Char holds a single character. Trying to stuff two hyphens into a Char - for shame.
Next, be aware of your tools. The Split function accepts a second parameter that controls how empty elements should be handled. It also includes overloads for accepting a string array.
Dim fileParts As String() = fileInfo.Name.Split(
New Char() {"_", "-"},
StringSplitOptions.RemoveEmptyEntries)

How to set decimal separator in MS Access using VBA?

My software creates PAIN001.XML files directly from an Access financial database. The decimal separator must always be a dot. The numbers are formatted with:
MyText = Format(MyNumber, "#0.00")
However, the format string's dot is automatically replaced by the system decimal separator, which might be "," instead of "." !
In Excel there are easy solutions, for example:
Application.DecimalSeparator = "."
...
However, MS Access doesn't recognize this application property.
Is there a simple way to define a decimal separator within Access vba code ?
Of course, one can create a function which scans each MyText number for wrong decimal separators and replaces them with a dot, but this function would have to be called separately for each number, slowing down the code quite a lot…
The decimal separator must always be a dot.
Then use Str:
MyText = Str(MyNumber)
To convert such a string to a number use Val:
MyNumber = Val(MyText)
I guess the problem is not solveable with the decimal separator Application.DecimalSeparator = ".", even if it was supported by the Access library. It is a rather complicated issue, for the non-US users, as we are used to have , as a decimal separator.
In general, VBA considers only . as a decimal separator. Without taking care of the application default separator, the location of the user and their settings. Thus, some interesting cases could happen:
Sub TestMe()
Dim myText As String
myText = "123,42"
Debug.Print Replace(Format(myText, "#0.00"), ",", ".")
End Sub
A possible solution, that I have implemented some time ago was to use Replace() and to replace as in the gif above. It could be a bit slow indeed, but taking into account the usage of VBA and Access, extreme speed is not something the app could achieve anyway.

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.

Data download from PHP is not split into newlines

I am retrieving data from web. Data is seperated by each line. Data looks like this
Data1
Data2
Data3
I want to alert for each data found on the webpage. Tried this,
Dim Lines() As String
Dim stringSeparators() As String = {vbCrLf}
Dim Source As String
Dim wc As New WebClient
Source = wc.DownloadString("http://www.example.com/data.php")
Lines = Source.Split(stringSeparators, StringSplitOptions.None)
For Each s As String In Lines
MsgBox(s)
Next
But unfortunately, it alerts once all the data. My question is, how to alert for each data ?
vbCrLf, as defined in Constants, won't match a single UNIX-style newline - "Newline" (\n), LF/LINEFEED, ASCII 10 - character as transmitted from PHP.
To deal with both Windows and UNIX/Linux end-of-line sequences, use:
Dim stringSeparators() As String = {vbLf, vbCrLf}
The order the separators supplied does not matter, see the remarks in String.Split for details.
While the above solves the problem in a fairly robust manner, it may better to use the exact EOL format, especially when writing - and to make a selection prior based on established format. In this case that might be only using vbLf which would work for the given PHP output, but would incorrectly leave in CR characters for Windows text files.
When dealing with system-native text files, or Windows components such as Controls, vbNewLine should generally be preferred over vbCrLf: vbCrlLf is appropriate when the goal is to be explicit, as above, and only accept/emit a specific ASCII sequence as mandated by protocols and conventions.
When dealing with whitespace characters, I often end up running the String.Asc() method on them, to see what they really are.
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.asc(v=vs.110).aspx
Then, you can ensure that you are splitting on the correct character.

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.