Using Split in Vb.net in last dash - vb.net

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.

Related

Get a particular string from the main string in VB.net

substring = "frTrig_worldcup"
main string = "0.10/BMM/fTrig_Mast/BOYU/frTrig_worldcup"
The substring value will change. "frTrig_xxxx" but "frTrig_" will be constant
I need to get the substring which is in the LAST PART of the main string. The issue is the "frTrig_" comes in mid of the string also
Please help me to solve this.
We can try using String#split here:
Dim input As String
Dim parts() As String
input = "0.10/BMM/BOYU/fTrig_MastrrRea0.01/frTrig_worldcup"
parts = input.Split("/")
MsgBox(parts(parts.Length-1))

Parsing through an Array For Next loop Visual Basic

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...

vb.net split last part of the string

net 2.0 and need to split the last / mark of the string. Currently I have a code that says Dim test As String = "Software\Microsoft\Windows\Welcome" and need a code that will split that Software\Microsoft\Windows\Welcome into two separate section at the end
So I'll have Software\Microsoft\Windows and Welcome as new strings.
I can only find things that will split the beginning from the rest like
Dim whole As String = "Software/Microsoft/Windows/Run"
Dim firstpart As String = whole.Substring(0, whole.IndexOf("/"))
Dim lastpart As String = whole.Substring(whole.IndexOf("/") + 1)`
Use String.LastIndexOf()
Dim whole As String = "Software/Microsoft/Windows/Run"
Dim firstpart As String = whole.Substring(0, whole.LastIndexOf("/"))
Dim lastpart As String = whole.Substring(whole.LastIndexOf("/") + 1)
Try splitting with '\' As your delimeter and store it as a string array. Then just grab the last element and it should be "Welcome".

How do I detect for a specific character in a string in VB.NET?

Okay, so in a program I'm working on in VB.NET I'm trying to make it so I can take in a list of strings (each on a different line). For each line I want to take in the line, and break it up into three parts. The first part goes from the beginning of the string to the first colon in the string, the second part goes from the first colon to the at symbol, and the last part goes from the at symbol to the end of the string.
For example, I'd take in a line of the series of lines:
hello:world#yay
I'd want to break it into three separate strings of "hello", "world", and "yay".
How would I do such a thing in VB.NET?
You can accomplish this with a Split. For example purposes, I am re-splitting a string which I could have saved off, so I wouldn't have to Split it again. However, it's simpler to understand this way:
Dim s as String = "hello:world#yay" 'This can be a string from a loop.
Dim hello As String = s.Split(":")(0) 'Get everything before colon.
Dim world As String = s.Split(":")(1).Split("#")(0) 'Get everything after colon, and split the result again, grabbing everything before the amp.
Dim yay As String = s.Split(":")(1).Split("#")(1) 'Get everything after colon, and split the result again, grabbing everything after the amp.
If you're reading from a text file, e.g.
Dim objReader As New StreamReader("c:\test.txt")
Dim s As String = ""
Dim hello As String
Dim world As String
Dim yay As String
Do
s = objReader.ReadLine()
If Not s Is Nothing Then
hello = s.Split(":")(0)
world = s.Split(":")(1).Split("#")(0)
yay = s.Split(":")(1).Split("#")(1)
End If
Loop Until s Is Nothing
objReader.Close()
Use the split command
Start by splitting the string with the ":" and then split the second edit element with the "#"
Look at string.indexOf and take it from there

compare a string and trim in vb.net

I have this string that shall come in from another file. The string has maximum length of 102 digits. I need to compare the string with numbers in a pair and delete those from that string.
e.g - 6125223659587412563265... till 102
numbers that compare with this string-
first set - 61
new string = 25223659587412563265
second set - 36
new string = 252259587412563265
and so on. the set of numbers shall go to maximum of 51 pairs = 102, which shall give an end result of string = ""
How can i achieve this in a loop?
this is not answer, this is editing the question. i dont know why but the edit button just vaniashed so i have to edit question here.
No duplicates will ever be in this string. and in the end when compares are done, i want to see what numbers are left in pairs.
Dim input As String = "6125223659587412563265"
Dim targets As String() = {"61", "36"}
For Each target As String In targets
input = input.Replace(target, "")
Next
Debug.Assert(input = "252259587412563265")
Here is a simple solution. You will need to add your pairs to the List(Of String) and also initialize input to the string you want to alter.
Dim pairs As New List(Of String)()
Dim input As String = String.Empty
For Each pair As String In pairs
input = input.Replace(pair, String.Empty)
Next