How to delete a specific and not constant part of a string - vb.net

I would like to cut this string
Dim str = "BMW{1}/X5{5}/image"
like this
Dim Brand = "BMW"
Dim idBrand= 1
Dim Model = "X5"
Dim Resource = "image"
for the / part I can easily do str.Split("/") but for the {1} I don't know if there is a special function or not.

This is a good place to use regular expressions:
Dim str = "BMW{1}/X5{5}/image"
Dim regex as new RegEx("(?<brand>[^{]+){(?<idbrand>\d+)}\/(?<model>[^{]+){[^}]+}\/(?<resource>.*)")
Dim match = regex.Match(str)
match is now an object containing various bits of information about what it's found. Since I added named capturing groups into the regex (brand, idbrand, model and resource), you can now access those within the match object:
Dim Brand = match.Groups("brand").Value
Dim idBrand= match.Groups("idbrand").Value
Dim Model = match.Groups("model").Value
Dim Resource = match.Groups("resource").Value
There is lots of information on regex available on the internet, one resource I find useful is regex101.com, which will give you an explanation of what the regex is doing on the right hand side.

Related

VB Loading a list of variables from a text document

I'm currently trying to load a list of variables that are formatted like this:
5,
6,
3,
3,
etc, and I'm trying to output them to variables like this:
Strength = variablesList(1)
Agility = variablesList(2)
But so far, I've not been able to find a solution that seems to work for what I'm trying to do.
I'm currently working with:
Dim destination As String = Environment.GetFolderPath("C:\Roll20Output\Class" + outputClass + "2.txt")
Dim FileReader1 As New StreamReader(destination)
Dim Contents1 As String
Dim index As Integer = 0
While FileReader1.Peek <> -1
Contents1 = FileReader1.ReadLine
Dim array As New ArrayList
array.AddRange(Contents1.Split(","))
variablesList.Add(array)
End While
Strength = variablesList(1)
Agility = variablesList(2)
But so far I can't seem to get anything to output.
Would anyone be able to help?
Thanks
You are using a lot of outdated stuff in your code (reading a file with StreamReader, ArrayList instead of List<T>, etc.). I would suggest the following (untested):
' Returns an array with one string per line
Dim lines = File.ReadAllLines("C:\...\SomeFile.txt")
' Remove trailing `,` - LINQ magic
lines = (From s In lines Select s.TrimEnd(","c)).ToArray()
Dim strength = CInt(lines(0))
Dim agility = CInt(lines(1))
...
If you get rid of the useless trailing commas, you can skip the second step. If you use only commas instead of new lines, the first step becomes:
Dim lines = File.ReadAllText("C:\...\SomeFile.txt").Split(","c)

How to get list of printers not printing direct?

I need a list of printers that DO NOT print direct. Getting a list that do print direct seems reasonably easy. But how to do the opposite?
Dim PrintServer As New SysPrint.PrintServer
Dim arrFlags(0) As SysPrint.EnumeratedPrintQueueTypes
arrFlags(0) = System.Printing.EnumeratedPrintQueueTypes.DirectPrinting
Dim QColl As SysPrint.PrintQueueCollection = PrintServer.GetPrintQueues(arrFlags)
PrintServer.GetPrintQueues Method
EnumeratedPrintQueueTypes Enumeration
MSDN says that the EnumeratedPrintQueueTypes has a FlagsAttribute attribute that allows a bitwise combination of its member values. So I should be able to specify NOT direct somehow. How do I do it?
I tried to do this arrFlags(0) = Not System.Printing.EnumeratedPrintQueueTypes.DirectPrinting but that returned no results. Clearly incorrect.
So how do I manipulate the flags attribute to eliminate all printers printing direct?
This is one way to do it but it seems very inelegant:
'get full list
Dim PrintServer As New SysPrint.PrintServer
Dim QColl As SysPrint.PrintQueueCollection = PrintServer.GetPrintQueues()
'get those not printing direct
Dim Qcoll2 As List(Of SysPrint.PrintQueue) = QColl.Where(Function(x) Not (x.IsDirect)).ToList
'select name only
Dim strList As List(Of String) = Qcoll2.Select(Function(x) x.Name).ToList

Split a String into 2 Variables

What I'm trying to do here is Capture 2 Variables from a Textbox
Here is an example of whats going to be in here.
User:Pass
I want to declare everything before the : as user and everything after the : as pass.
I've Googled, and found a few things, but I couldn't seem to get it working fully.
Dim words As String() = textbox1.text.Split(":")
Dim user as String = words(0)
Dim pass as String = words(1)
Dim str = "User:Pass"
Dim split = str.Split(":")
Dim user as String
Dim password as String
If (split.Count = 2) then
user=split(0).ToString()
password = split(1).ToString()
End If
Split on the :, if there are 2 entries in the resulting array, populate the user variable with the first item, and the password variable with the second.
Have a look at the split function.
http://msdn.microsoft.com/en-us/library/6x627e5f%28v=vs.80%29.aspx
Dim user As String
Dim pass As String
Dim iPosEQ As Integer
iPosEQ = textbox1.text.IndexOf(":", System.StringComparison.Ordinal)
kv(0) = textbox1.text.Substring(0, iPosEQ - 1)
kv(1) = textbox1.text.Substring(iPosEQ + 1)
This works even with passwords (or users) with ":"

Adding values to array

I am trying to run an event which will search through the different files in a given directory. The goal is to have it search for all files that begin with 'SP_', which are .sql files containing Stored Procedures. I would then like to add the full text of these Procedures to an array to be used later. This is causing an error when run, which I believe is because 'FullProcedureArray()', the string array I am trying to load does not have defined boundaries. When I declare it as 'FullProcedureArray(7)', or with some other value, it appears to run fine. But I don't want to have to hard-code a boundary for 'FullProcedureArray'; I would rather let it be defined by whatever the number of files in the folder is.
My question: Is there a way to declare 'FullProcedureArray' without having to give it an absolute value? I may just be missing something painfully obvious, but I haven't worked with this type of array much in the past. Thanks in advance for your help.
Dim AppDataLocation As String = "C:\Files\TestFiles\"
Dim ProcedureArray As String()
Dim ProcedureText As String
Dim FullProcedureArray() As String
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation)
Dim fileSystemInfo As System.IO.FileSystemInfo
Dim i As Integer = 0
For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
If (fileSystemInfo.Name.Contains("SP_")) Then
ProcedureArray = System.IO.File.ReadAllLines(AppDataLocation & fileSystemInfo.Name)
ProcedureText = Join(ProcedureArray, "")
FullProcedureArray.SetValue(ProcedureText, i)
i = (i + 1)
End If
Next
An array by definition has a fixed upper bound. If you don't want a fixed upper bound, don't use an array. Use, for example, a List(Of String) instead:
Dim AppDataLocation As String = "C:\Files\TestFiles\"
Dim ProcedureList As New List(Of String)
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation)
For Each fileSystemInfo As System.IO.FileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
If (fileSystemInfo.Name.Contains("SP_")) Then
Dim ProcedureText As String = _
System.IO.File.ReadAllText(AppDataLocation & fileSystemInfo.Name)
ProcedureList.Add(ProcedureText)
End If
Next
If, for some reason, you still need the result as an array afterwards, simply convert the list to an array:
Dim myArray() As String = ProcedureList.ToArray()
If you don't want to give a size to your array or want to change at runtime, you can use "Redim Preserve"
http://msdn.microsoft.com/en-us/library/w8k3cys2%28v=vs.71%29.aspx

How do you import a particular record in a database to your system and convert it to string for comparison?

Here's the deal, it's a log in form and I need to put case-sensitive validation, in other words if your Username = Admin then Admin != admin
Consider this code block (VB is really unfamiliar for me so break it to me gently ^^)
This is after it has matched a record in the database to the parameter passed to the function LogIn()
If dataTable.Rows.Count > 0 Then
'case-sensitive Validation follows
'data in Column ID and Password are placed in variables
'which are then compared to the arguments sent using the Compare() function
Dim strID = dataTable.Columns("U_ID").
Dim strPass = dataTable.Columns("Password")
Dim idResult As Integer 'both results will hold the value of String.Compare()
Dim passwordResult As Integer
*idResult = String.Compare(strID, ID)
the asterisk-ed line returns an error (obviously) as strId is not of data type String.
That's my dilemma.
I also tried using LIKE but again since strId and strPass are not Strings, all I get is an error.
Change this line:
Dim strID = dataTable.Columns("U_ID")
to this:
Dim strID as String = dataTable.Columns("U_ID").ToString