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

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)

Related

How to insert text into a string at a point of a specific character(s)

I have searched the whole internet, and the only things I can find is IndexOf. The issue with that is I need a way to put the insert at a specific one of those chars.
I am currently using this
RichTextBox1.Text = RichTextBox1.Text.Insert(RichTextBox1.Text.Substring(0,RichTextBox1.Text.Split("^")(CurrentSlide).Length), "^")
Which of course is completely incorrect after thinking about it, because the length of the index is not the real length of the text to the index.
I think you have the right idea. It seems like you have a "^" delimited string, and you want to insert an empty at some point. Since a collection is easier to work with than a string, your Split is a good start.
Dim parts = RichTextBox1.Text.Split("^")
Let's make that a list, so we can insert into it:
Dim parts = RichTextBox1.Text.Split("^").ToList()
Now, just insert an empty string where you want the new element:
Dim parts = RichTextBox1.Text.Split("^").ToList()
parts.Insert(CurrentSlide, "")
And combine them back for editing in your textbox:
Dim parts = RichTextBox1.Text.Split("^").ToList()
parts.Insert(CurrentSlide, "")
RichTextBox1.Text = String.Join("^", parts)

Why isn't my String updating?

I have a small string variable with a length of 400-500 characters spaces included. I've tried a few things as far as removing some parts of the string, while in a Do/While loop. I'm looking to go through the loop, then remove 300 characters from it, however it doesn't seem to be actually removing it from the string.
Are we not actually able to modify a string object and must be forced to SubString the text variable to get the desired result?
Do while stringText.Length >= 300
'stringText.replace(textToRemove, "") This doesn't replace the string variable
'stringText.Remove(0,299) This also doesn't remove the specified range of characters
Loop
Strings in .NET and VB are immutable, meaning that a string can never change once it's been defined. What the various Replace/Remove methods do is return a new, modified string, which you can store to the same variable.
Like this:
Do while stringText.Length >= 300
stringText = stringText.Replace(textToRemove, "")
Loop
It's important to note, though, that this is potentially expensive - a new string object is allocated. If you have a lot of modifications to make to a string separately, each one will create a new copy, and for large strings it might create unnecessary memory allocations.
For this reason, we have the System.Text.StringBuilder class (as mentioned by roryap), which lets us manipulate strings directly. Read up on it.
These functions return a value, you must assign the value returned to the original string for it to be updated:
e.g.
stringText = stringText.replace(textToRemove, "")
Otherwise you are just discarding the value returned - the functions do not mutate the original string
You have to assign string for the return value
stringText =stringText.replace(textToRemove, "")

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.

Isolate a a substring within quotes from an entire line

To start here is an example of a line I am trying to manipulate:
trait slot QName(PrivateNamespace("*", "com.company.assembleegameclient.ui:StatusBar"), "_-0IA") type QName(PackageNamespace(""), "Boolean") value False() end
I wrote a code that will go through and read through each line and stop at the appropriate line. What I am trying to achieve now is to read through the characters and save just the
_-0IA
to a new string. I tried using Trim(), Replace(), and indexof so far but I am having a ton of difficulties because of the quotation marks. Has anyone deal with this issue before?
Assuming your source string will always follow a strict format with only some data changes, something like this might work:
'Split the string by "," and extract the 3rd element. Trim the space and _
quotation mark from the front and extract the first 5 characters.
Dim targetstr As String = sourcestr.Split(","c)(2).TrimStart(" """.ToCharArray).Substring(0, 5)
If the length of the target string is variable it can be done like this:
Dim temp As String = teststr.Split(","c)(2).TrimStart(" """.ToCharArray)
'Use the index of the next quotation mark instead of a fixed length
Dim targetstr As String = temp.Substring(0, temp.IndexOf(""""c))