I have a VendorID in the following format "24849_1". I'm trying to split the "1" off by itself and populate a field with it. The following code is what I'm using but all I'm managing to do is drop the "1" off completely. Anyone have any suggestions?
Dim strCustom1 As String
Dim strVendorID As String
strCustom1 = pWorkdoc.Fields("VendorID").Text
If strCustom1 <> "" Then
strCustom1 = Split(strCustom1, "_") (0)
xmlDoc.getElementsByTagName("RemitTo").Item(0).Text = strCustom1
End If
You're calling Split but have you actually bothered to find out what it does? It returns an array containing the split parts. You are then getting the part at index 0 in that array. If you actually want the part at index 1, i.e. the second part, then you should use index 1 instead of index 0.
The Split will give you an array, and you are taking the first element only; what you can do is:
strCustom1 = pWorkdoc.Fields("VendorID").Text // let it be 24849_1
If strCustom1 <> "" Then
Dim resultArray = Split(strCustom1, "_")
//resultArray[0] will be 24849
//resultArray[1] will be 1
//assign them as you need
End IF
The Split function returns a String array, but you have only taken the first element (the second element which contains the "1" was discard).
Keep the split array in a variable, so that you can access both elements.
Dim strCustom1 As String = "24849_1"
Dim split As String()
split = strCustom1.split("_")
Console.WriteLine("Part1={0} Part2={1}", split(0), split(1))
Related
i have two example
"hcg.com.ph?C402-10A-2012-06132017-22"
"hcg.com.?C3032-1B-2012-06132017-1"
output should be
hcg.com.ph?C402-10A-2012-06132017
hcg.com.?C3032-1B-2012-06132017
but i got
hcg.com.ph?C402 and hcg.com.?C3032
Dim FinalSplt() As String
Dim ItemBaseCode As String
FinalSplt = value.ToString.Split("-")
ItemBaseCode = FinalSplt(0)
How to split in the last dash?
Here is some code that uses Substring and LastIndexOf.
'test input
Dim si() As String = {"hcg.com.ph?C402-10A-2012-06132017-22", "hcg.com.?C3032-1B-2012-06132017-1"}
'use to verify
Dim sv() As String = {"hcg.com.ph?C402-10A-2012-06132017", "hcg.com.?C3032-1B-2012-06132017"}
For x As Integer = 0 To si.Length - 1
Dim s As String = si(x).Substring(0, si(x).LastIndexOf("-"c))
'verify
If s = sv(x) Then
Stop 'verified
End If
Next
Ok, without actually writing code I can see you need to split the string more efficiently.
Firstly, strip the quotes.
Secondly, split the string based on the ? mark.
Take the second string in the array, and split that based on the - mark.
Now you have an array of all the portions, join this array with all except the last element.
Join the new string with the original first part.
Add the quotes back if needed.
I am stuck here. Spent hours trying many different approaches but nothing is working
I have an array that holds text that looks like this
4456|4450|17
4466|4430|18
4446|4420|19
4436|4410|20
The separator is a pica ("|").
What I am trying to do is run through the array and extract the first two columns in separate strings to compare the values, look for the max, and min.
I am trying to end up with a string like this
4456,4466,4446,4436
Here is the solution:
Dim source As String = prices
Dim stringSeparators() As String = {vbCrLf}
Dim result() As String
result = source.Split(stringSeparators,
StringSplitOptions.RemoveEmptyEntries)
Dim fString As String = String.Join(Of String)(", ", result.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(0)))
MsgBox(fString)
Let's take your example below...
4456|4450|17
4466|4430|18
4446|4420|19
4436|4410|20
prices = [the array shown above]
For Each i As String In prices
high = (i.Split("|"))(0)
highs = highs & highs1 & ","
MsgBox(highs)
Next
The reason you are getting 4,4,5,6,,4,4,5,0,,1,7 is because for each string you are splitting on the | and then taking the first character adding a comma to it.
If you want to get the first column or index whatever you want to call it before the | you need to loop through each string in that array and select out the values...
'this is my test array...
Dim arr As New ArrayList From {"4456|4450|17", "4466|4430|18", "4446|4420|19", "4436|4410|20"}
Now we can use a String.Join function, cast the array for each item as a string and finally select the first item on the split. This will get every item before the | and put them in a string separated with a comma.
Dim fString As String = String.Join(Of String)(", ", arr.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(0)))
If you want the second section select the 1st index as arrays start at 0...
Dim sString As String = String.Join(Of String)(", ", arr.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(1)))
Here is my screenshot of the outputs...
Hello I am trying trying to use the contains method to see if a list-block (which exclusively consists of filepaths) 'contains' a certain string. It is supposed to remove the respective entry (in a for loop) if the string is contained in the filepath.
Dim index As Integer = findNumFiles("Archer", 6) '//returns number of files in archer directory
Dim iString As String = "Archer"
For i = 0 To index
If Lookup.Items(index).contains(iString) Then
removeFromList(Lookup, index) '//passes "Lookup" as a ListBlock name and removes the index number
End If
Next
a sample filepath would be
"Z:\Movies and TV\Archer\Season 6\1.mp4"
edit: I forgot to mention that it does not work. I tested the command with a list entry simply named "archer" and if the iString is = to "archer", that list entry is removed. It seems the fact that I'm attempting to use the contains method on a filepath is the problem but I'm not sure.
Thanks for any insight!
Use Instr function to check if the string present or not,
I am not sure about what "Lookup.Items" .
Use 'i'in for loop instead of index
Hope below code will help you
Sub test1()
Dim index As Integer
index = findNumFiles("Archer", 6)
'//returns number of files in archer directory
Dim iString As String
iString = "Archer"
For i = 0 To index
If InStr(1,Lookup.Items(i), iString, 1) > 0 Then
' removeFromList(Lookup, index) '//passes "Lookup" as a ListBlock name and removes the index number
End If
Next
End Sub
I have a csv file I need to parse and get an item out of the middle of it. I have chose string manipulation to do this.
sample input file data would be..
Nick,frog,snake,1234
I can get the last entry "1234" with this code..
line.Substring(line.LastIndexOf(",") + 1)
How would I get the 3rd entry, "snake", with substrings? (what the OP means is: how can I get the third element in the comma-separated string, which happens to be "snake")
If you've stored the text of your file in myInputText, and you want to access the third item, the following will work:
resultString = Strings.Split(myInputText, ",")(2)
You can access any string in the file this way.
How would I get the 3rd entry "snake" with substrings?
The easiest way would obviously be with split:
Dim teststr As String = "Nick,frog,snake,1234"
Dim teststr2 As String = teststr.Split(","c)(2)
A simple function like this, using substring, will do the same thing:
Dim teststr3 As String = GetStr(teststr, 2)
Private Function GetStr(input As String, index As Integer) As String
input += ","
Dim counter As Integer = 0
If index <> 0 Then
Do
input = input.Substring(input.IndexOf(","c)).TrimStart(","c)
counter += 1
Loop Until counter = index
End If
GetStr = input.Substring(0, input.IndexOf(","c))
End Function
i got this far ... my data string, "num_str" contains a set of ~10 numbers, each separated by a comma. the last part of the string is a blank entry, so i use '.Trim' to avoid an error
Dim i As Integer
Dim m_data() As String
m_data = num_str.Split(",")
For i = 0 To UBound(m_data)
If m_data(i).Trim.Length > 0 Then
MsgBox(Convert.ToInt32(m_data(i).Trim))
End If
Next i
as you can see from the Msgbox, each of the numbers successfully pass through the loop.
where i am stuck is how to place all of the 'Convert.ToInt32(m_data(i).Trim)' numbers, which are now presumably integers, into an array.
how do i build an array of integers inside the For / Next loop so i can find MAX and MIN and LAST
TIA
You just need to initialize the array with the zero-based indexer. You can deduce it's initial size from the size of the string():
Dim m_data = num_str.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim intArray(m_data.Length) As Int32
For i = 0 To m_data.Length - 1
intArray(i) = Int32.Parse(m_data(i).Trim())
Next i
Note that i've also used the overload of String.Split which removes empty strings.
This way is more concise using the Select LINQ Operator.
Dim arrayOfInts = num_str.
Split({","c},
StringSplitOptions.RemoveEmptyEntries).
Select(Function(v) Int32.Parse(v.Trim()))
Dim minInt = arrayOfInts.Min()
Dim maxint = arrayOfInts.Max()