how to add some string to all array item? - arrays

I have an array like below
(0) = "apple"
(1) = "orange"
How can I add some string to all item in array? like apple become 'apple', orange become 'orange'
Edited
Private Sub test()
Dim txtReader As TextReader = New StreamReader("data.csv")
Dim parser = New CsvParser(txtReader)
Dim str As String = ""
'Ignore first line
parser.Read()
While True
Dim row = parser.Read()
If row Is Nothing Then
Exit While
End If
str &= $"({String.Join(",", row)}),"
End While
str_record = str.TrimEnd(",")
End Sub
Private Sub Model_Insert()
Dim data As String = ""
Dim query As String = "INSERT INTO main_item(item_code,item_name,item_desc,item_unitprice,item_barcode,dept_id,cat_id,gst_id,set_item,active)" &
"VALUES " & str_record & ""
Using cmd = New MySqlCommand(query, conn)
cmd.ExecuteNonQuery()
End Using
End Sub
Im trying to create a string and use it in INSERT INTO

Use a For-loop:
Dim array = {"apple", "orange"}
For i As Int32 = 0 To array.Length - 1
array(i) = $"'{array(i)}'"
Next
If you can't use string interpolation yet, use String.Format (or string concatenation):
For i As Int32 = 0 To array.Length - 1
array(i) = String.Format("'{0}'", array(i))
Next

If you don't mind recreating the array, you could use Linq Select:
Dim testarray As String() = New String() {"orange", "apple"}
testarray = testarray.Select(Function(x) String.Format("'{0}'", x)).ToArray()

Related

Iterating Variant array in VBA with For Loop

I tried to concat some strings in Excel with VBA.
Sub Button1_Click()
Dim lenArray As Variant
Dim strArray As Variant
Dim strCombined As String
Dim space As String
lenArray = Range("D1:J1").Value
strArray = Range("D2:J2").Value
space = " "
Dim i As Integer
Dim temp As String
For i = LBound(strArray) To UBound(strArray)
strCombined = strCombined & strArray(i)
temp = WorksheetFunction.Rept(space, CInt(lenArray(i)) - Len(strArray(i)))
strCombined = strCombined & temp
Next i
Range("B4").Value = strCombined
End Sub
However, it always gives me out of bound error.
I also tried changing
For i = LBound(strArray) To UBound(strArray)
to
For i = 1 To 7
or
For i = 1 To 2
but it never works.
I have tried to iterate only the strArray with For Each and it would work.
Any help would be great.
Edited:
The spreadsheet I am using

VB - Assigning Strings in an Array

I am trying to assign values to the strings in a comma separated string.
See incorrect code below.
Dim newArray As String() = "M2-1_,IR,Pass,499V,>10G,5.0s"
results = Split(newArray, ",", -1, vbBinaryCompare)
Dim results1 As String = newArray(0)
Dim results2 As String = newArray(1)
Dim results3 As String = newArray(2)
ListBox1.Items.Add(results1)
ListBox1.Items.Add(results2)
ListBox1.Items.Add(results3)
My current results are:
M
2
-
I would like the results:
M2-1_
IR
Pass
Thanks!!!!!
If you want just the first 3:
Dim newArray As String = "M2 - 1_,IR,Pass,499V,>10G,5.0s"
ListBox1.Items.AddRange(newArray.Split(",").Take(3).ToArray)
If you want them all:
Dim newArray As String = "M2 - 1_,IR,Pass,499V,>10G,5.0s"
ListBox1.Items.AddRange(newArray.Split(","))
Dim newArray = "M2-1_,IR,Pass,499V,>10G,5.0s"
Dim results() As String = newArray.Split(",")
ListBox1.Items.Add(results1(0))
ListBox1.Items.Add(results2(1))
ListBox1.Items.Add(results3(2))
Your code works if you index into the results array when adding to the ListBox.
Dim newArray As String = "M2-1_,IR,Pass,499V,>10G,5.0s"
Dim results() = Split(newArray, ",", -1, vbBinaryCompare)
ListBox1.Items.Add(results(0))
ListBox1.Items.Add(results(1))
ListBox1.Items.Add(results(2))

VB.NET Run command against 2 arrays

I have read 2 text files into 2 arrays and simply want to run a command which uses the 2 arrays.
Example:
part1.txt(array 1)
hxxp://somethinghere.com\1
hxxp://somethinghere.com\2
part2.txt(array 2)
Bob
James
myprogram.exe hxxp://somethinghere.com\1 Bob
myprogram.exe hxxp://somethinghere.com\2 James
I simply want to run a for loop which goes through both arrays, here's what I have so far:
Dim part1() As String = IO.File.ReadAllLines("C:\part1.txt")
Dim part2() As String = IO.File.ReadAllLines("C:\part2.txt")
For Each line As String In part1
MsgBox(line)
Next
EDIT:
Working code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim part1() As String = IO.File.ReadAllLines("C:\part1.txt")
Dim part2() As String = IO.File.ReadAllLines("C:\part2.txt")
For parse As Integer = 0 To part1.GetUpperBound(0)
MsgBox(String.Concat("myprog.exe " & """" & part1(parse) & """" & " -arg1 " & """" & part2(parse) & ".txt" & """"))
Next
End Sub
So it will look like this:
myprog.exe "hxxp://somethinghere.com" -arg1 "Bob.txt"
How about something like this?
Private Sub testMethod()
Dim part1() As String = New String() {"one", "two", "three"}
Dim part2() As String = New String() {"10", "20", "30"}
For parse As Integer = 0 To part1.GetUpperBound(0)
Debug.Print(String.Concat("command ", part1(parse), part2(parse)))
Next
End Sub
Take a look at Enumerable.Zip:
Enumerable.Zip(Of TFirst, TSecond, TResult) Method
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
So you could so something like
Dim part1() As String = IO.File.ReadAllLines("C:\part1.txt")
Dim part2() As String = IO.File.ReadAllLines("C:\part2.txt")
For Each element In part1.Zip(part2, Function(a, b) New With {a, b})
MsgBox(String.Format("{0} - {1}", element.a, element.b))
Next
First check that the arrays are the same length, then you can loop the indexes of one array and access the corresponding item in the other array.
To run the program you can use a ProcessStartInfo object to specify the parameters:
Dim part1() As String = IO.File.ReadAllLines("C:\part1.txt")
Dim part2() As String = IO.File.ReadAllLines("C:\part2.txt")
If part1.Length = part2.Length Then
For i As Integer = 0 To part1.Length - 1
Dim p As New ProcessStartInfo
p.FileName = "myprog.exe";
p.Arguments = """" & part1(i) & """ -arg1 """ & part2(i) & ".txt"""
Process.Start(p);
Next
Else
' Oops! different number of items in the files
' Tell the user
End If
You can set more properties in the ProcessStartInfo object to control how the program is started.

get every other value of string array

I am trying to populate a combo box with every other element of an array
the array contains data like this
(0) server
(0) sqlInstance
(1) server
(1) sqlInstance
(2) server
(2) sqlInstance
I want all sqlInstance values to be added to the combo box. Not sure how to do it other than maybe another array.
Thanks
Private Sub GetSQLServers()
Dim oSQLApp As New SQLDMO.Application
Dim oServerNames As SQLDMO.NameList
Dim strServer As String
Dim ServerArr() As String
oServerNames = oSQLApp.ListAvailableSQLServers()
For i = 0 To oServerNames.Count
strServer = (oServerNames(i))
ServerArr = strServer.Split("\")
txtSQLHost.Text = ServerArr(0)
For Each s As String In ServerArr
'value of server instance
cbxSQLServerNames.Items.Add(s)
Next
Next i
End Sub
This is working, I'm pretty sure it is not the best way
The combo box fills with sqlInstances only
The txtbox has the server
oServerNames is server\sqlInstance
Private Sub GetSQLServers()
Dim oSQLApp As New SQLDMO.Application
Dim oServerNames As SQLDMO.NameList
Dim strServer As String
Dim ServerArr() As String
Dim sn As Integer = 1
oServerNames = oSQLApp.ListAvailableSQLServers()
For i = 0 To oServerNames.Count
strServer = (oServerNames(i))
ServerArr = strServer.Split("\")
txtSQLHost.Text = ServerArr(0)
For Each s As String In ServerArr
'value of server instance
If ((sn Mod 2) = 0) Then
cbxSQLServerNames.Items.Add(s)
sn = sn + 1
Else
sn = sn + 1
End If
Next
Next i
End Sub
Using this now..
Thanks
Private Sub GetSQLServers()
Dim oSQLApp As New SQLDMO.Application
Dim oServerNames As SQLDMO.NameList
Dim strServer As String
Dim ServerArr() As String
oServerNames = oSQLApp.ListAvailableSQLServers()
For i = 0 To oServerNames.Count
strServer = (oServerNames(i))
ServerArr = strServer.Split("\")
txtSQLHost.Text = ServerArr(0)
For Each item In oServerNames
Dim parts = item.Split("\")
Dim instance = parts(1)
cbxSQLServerNames.Items.Add(instance)
Next
Next i
End Sub
Try this:
For i = 0 To oServerNames.Count - 1 Step 2
Dim serverInstance = oServerNames(i) & "\" & oServerNames(i + 1)
cbxSQLServerNames.Items.Add(serverInstance)
Next i
The Count - 1 because, if you are count 10 items, you are counting from 0 to 9.
After looking at the source of your question, you say the array looks likes this:
(0) server
(0) sqlInstance
(1) server
(1) sqlInstance
(2) server
(2) sqlInstance
This is not possible. There can not be multiple items with the same index. Something is missing here...
If the array look like this:
(0) server\sqlInstance
(1) server\sqlInstance
(2) server\sqlInstance
You can do that:
For Each item In oServerNames
Dim parts = item.Split("\")
dim instance = parts(1)
cbxSQLServerNames.Items.Add(instance)
Next

multidimentional array of strings in vb.net

I have a text file like:
[edit] the number of line is unknown, it could be hundreds of lines.
How would I store them in a multidimensional array? I want my array to look like:
sample(0)(0) = "--------"
sample(0)(1) = "Line1"
..and so on
sample(1)(0) = "--------"
sample(1)(3) = "Sample 123"
..and so on
What I have done so far was to open the file and store in a 1-dimentional array:
logs = File.ReadAllLines("D:\LOGS.TXT")
I have tried creating an Array of string like:
Dim stringArray as String()()
stringArray = New String(varNumber0)(varNumber1)
But it returns and error.
You can use File.ReadLines/File.ReadAllLines to get the lines and a simple For Each-loop to fill a List(Of List(Of String)). Then you can use
list.Select(Function(l) l.ToArray()).ToArray()
to get the String()() (jagged array):
Dim lines = File.ReadLines("D:\LOGS.TXT")
Dim list As New List(Of List(Of String))
Dim current As List(Of String)
For Each line As String In lines.SkipWhile(Function(l) Not l.TrimStart.StartsWith("----------"))
If line.TrimStart.StartsWith("----------") Then
current = New List(Of String)
list.Add(current)
Else
current.Add(line)
End If
Next
Dim last = list.LastOrDefault()
If last IsNot Nothing Then
If Not current Is last AndAlso current.Any() Then
list.Add(current)
ElseIf Not last.Any() Then
list.Remove(last) ' last line was ("----------")'
End If
End If
Dim stringArray As String()() = list.Select(Function(l) l.ToArray()).ToArray()
If you want to include the --------- in the array at the first position:
For Each line As String In lines.SkipWhile(Function(l) Not l.TrimStart.StartsWith("----------"))
If line.TrimStart.StartsWith("----------") Then
current = New List(Of String)
current.Add(line)
list.Add(current)
Else
current.Add(line)
End If
Next
Try like this but you need to customize according to you
Dim mArray(10,10) As String
Dim i As Integer = 0
For I=0 to 10
For J=0 to 10
For Each line As String In System.IO.File.ReadAllLines("file.txt")
mArray(i,j) = cmdReader.Item(line)
Next
Next
Next
Use declaration like this (this is just a generic)
Dim dim1 As Integer = 0
Dim dim2 As Integer = 0
Dim strings(,) As String
Do
dim1 = NewDimensionNumberFromFile
dim2 = NewSecondDimensionNumberFromFile
ReDim Preserve strings(dim1, dim2)
strings(dim1, dim2) = ValueFromfile
Loop While (Not EOF()) 'this will determine

Resources