Convert .ToHashset Result from (Of Char) to (Of String)? - vb.net

I'm not real experienced in using Linq, and am trying to understand .ToHashSet.
I'm using VB for this.
I have this code:
' Import the list of Windows Classes to exclude; one per line.
Dim strRead As String = My.Computer.FileSystem.ReadAllText("C:\Exclude.txt")
' Split the string into a string array
Dim strExcludes As String() = strRead.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
' Convert the String Array to a HashSet (Traditional Method)
Dim lList As New HashSet(Of String)(strExcludes)
This works as intended; I get each line from the text file into the HashSet as 1 line per Key.
Now, I want to see how to do the same using .ToHashSet. The following code seems to work fine with the exception of it returning as type <Of Char>, so I get each character in it's own Key, rather than each line in it's own Key.
' Convert the String Array to a HashSet (Using System.Linq Method) (Unfinished: Works, but needs to be converted from Char to String)
Dim lListLinq = strRead.ToHashSet()
I've Googled, and fought with it a bit, trying to get it to return as type <Of String>, but not much Google info out there for .ToHashSet for VB, or really much in C# either regarding this particular problem. I'm not seeing where to do the conversion; .ToHashSet itself also accepts no arguments.
Anyone know what I'm missing? I have a feeling it's something real simple slipping by me.

Invoking ToHashSet() on a string will yield a HashSet(Of Char) and will not give the expected result as you've witnessed.
Instead, what you need to do is split the string returned by ReadAllText and then call ToHashSet() on it to get a HashSet(Of String).
strRead.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToHashSet()

Related

Vb Net check if arrayList contains a substring

I am using myArrayList.Contains(myString) and myArrayList.IndexOf(myString) to check if arrayList contains provided string and get its index respectively.
But, How could I check if contains a substring?
Dim myArrayList as New ArrayList()
myArrayList.add("sub1;sub2")
myArrayList.add("sub3;sub4")
so, something like, myArrayList.Contains("sub3") should return True
Well you could use the ArrayList to search for substrings with
Dim result = myArrayList.ToArray().Any(Function(x) x.ToString().Contains("sub3"))
Of course the advice to use a strongly typed List(Of String) is absolutely correct.
As far as your question goes, without discussing why do you need ArrayList, because array list is there only for backwards compatibility - to select indexes of items that contain specific string, the best performance you will get here
Dim indexes As New List(Of Integer)(100)
For i As Integer = 0 to myArrayList.Count - 1
If DirectCast(myArrayList(i), String).Contains("sub3") Then
indexes.Add(i)
End If
Next
Again, this is if you need to get your indexes. In your case, ArrayList.Contains - you testing whole object [string in your case]. While you need to get the string and test it's part using String.Contains
If you want to test in non case-sensitive manner, you can use String.IndexOf

Efficient way to get part of a string in vb.net

I have to parse a string to obtain a specific value in it.
Here's an example of a string I need to parse: "#MSG 12,9: NINJUTSU"
Here I need to obtain the 12 value. The order of the value will not change, meaning that I will always aim the first number in the string; however the length of the string (12, 9, 58) is variable (but never negative) and the message (NINJUTSU) is also changing.
So far I proceed like this:
Dim tempErrorList As List(Of String) = errorMsg.Split(New Char() {":"}).ToList()
Dim listErr As List(Of String) = tempErrorList(0).Split(New Char() {","}).ToList()
Dim errCode As List(Of String) = listErr(0).Split(New Char() {" "}).ToList()
However I don't like it because of the 3 splits needed to obtain the values. I do not know how I could do it in one shot or fewer operations?
Similar to the deleted answer, you could use String.Split like so: errorMsg.Split(" ,:".ToCharArray()), which does what you're doing above but with one function call.
errorMsg.Split(" ,:".ToCharArray())(1) would give you the "12" you need.
Alternatively, you can use combinations of String.SubString() with String.IndexOf(), but the logic can become unwieldy and opaque. String.Split Alternatives (MSDN) gives more details on this approach.

VB.NET - Convert String Representation of a Byte Array back into a Byte Array

I have an application that makes a call to a third party web API that returns a String that looks something like this:
"JVBERi0xLjMNCiXi48/TDQoxIDAgb2JqDQo8PA0KL1R5cGUgL091dGxpbmVzDQovQ291bnQgMA0KPj4NCmVuZG9iag0KMiAwIG9iag0KDQpbL1BERiAvVGV4dCAvSW1hZ2VDXQ0KZW"
(It's actually much longer than that but I'm hoping just the small snippet is enough to recognize it without me pasting a string a mile long)
The documentation says it returns a Byte array but when I try to accept it as a Byte array directly, I get errors. Part of my problem here is that the documentation isn't completely clear what the Byte array represents. Since it's a GetReport function I'm calling, I'm guessing it's a PDF but I'm not 100% sure as the documentation doesn't say at all.
So, anyway, I'm getting this String and I'm trying to convert it to a PDF. Here's what that looks like:
Dim reportString As String = GetValuationReport(12345, token.SecurityToken)
Dim report As Byte() = System.Text.Encoding.Unicode.GetBytes(reportString)
File.WriteAllBytes("C:\filepath\myreport.pdf", report)
I'm pretty sure that the middle line converts the String into a new Byte array rather than simply converting it into its Byte array equivalent but I don't know how to do that.
Any help would be fantastic. Thanks!
It looks like your string may be Base64 encoded, in which case you would use this to convert it to bytes:
Dim report As Byte() = Convert.FromBase64String(reportString)

vb.net string - replace placeholder tokens efficiently

I'm just looking for some ideas with a little problem. I've a string that's a template for a message, for example:
Dear [[Guest.FirstName]],
We hope your are looking forward to your holiday in [[Booking.ResortName]]
My current system for replacing the spaceholder tokens (e.g. [[Guest.FirstName]]) is very inefficient, there's loops inside loops and it takes for too long.
What I'm looking for is a way that would go through the string until it finds [[something]], then replace that [[something]] with it's real value, then continue on through the string.
Please note to replace [[something]] then I need to have access to the space holder (i.e. i need to know if it's [[Guest.FirstName]] or [[Booking.ResortName]]).
Any suggestions for an efficient way to achieve this would be very much appreciated, I feel it should be fairly straightforward but everything I can think of ends up with loops inside loops again.
Thanks!
Phil.
There are many factors which will affect performance, so it's hard to say, without knowing all the specifics, what method will be most efficient. However, when it comes to coding simplicity, using a RegEx MatchEvaluator would be an excellent option. I'm sure you'll agree that it's much cleaner than whatever you are currently using:
Public Function ReplacePlaceholders(data As String) As String
Dim r As New Regex("\[\[(?<placeholder>.*?)\]\]")
data = r.Replace(data, New MatchEvaluator(AddressOf PlaceHolderReplacementEvaluator))
End Function
Private Function PlaceHolderReplacementEvaluator(match As Match) As String
Dim name As String = match.Groups("placeholder").Value
Return LookUpValueByPlaceholderName(name) ' Replace with value lookup logic here
End Function
If the total number of placeholders in the data is going to be rather small, and the list of possible placeholders is small, it's probably best to just have a list of them with their values and replace them like this:
Public Function ReplacePlaceholders(data As String) As String
Dim placeHolders As Dictionary(Of String, String) = LoadPlaceHolders()
For Each i As KeyValuePair(Of String, String) In placeHolders
data = data.Replace(i.Key, i.Value)
Next
Return data
End Function
Private Function LoadPlaceHolders() As Dictionary(Of String, String)
Dim placeholders As New Dictionary(Of String, String)
' Load data here
Return placeholders
End Function
If you really want the most efficient solution, though, going character by character and appending, as you go, to a StringBuilder or an output Stream, is going to be your best option. It's not going to be pretty, but if you post what you have to CodeReview, there may be some people who could find ways of making it slightly less ugly :)

KeyNotFoundException when a Key with the requested value exists in VB.NET ASP.NET 2.0 Dictionary

The ValueList variable is defined as Dictionary(Of String, String)
The watch values captured show the data (tmpData) has a value of "1".
The ValueList is defined such that the replacement value for "1" is "Project Resource" as can be seen form the expansion of the SSGCol.ValueList Watch variable.
However, when I try to access the value I receive a KeyNotFoundException
I have done this sort of thing thousands of times in the past, but for some reason today I cannot get it to work. I must be missing something really obvious and need someone to take a fresh look and give me a slap when they see my obvious mistake.
Perhaps there are some unprintable characters in the string. Try comparing the bytes in the strings, for instance:
Dim bytes() As Byte = System.Text.Encoding.Unicode.GetBytes(tmpData)
This works for me...
Dim lst As New Dictionary(Of String, String)
lst.Add("1", "Test")
Dim tmpS As String = String.Empty
lst.TryGetValue("1", tmpS)