vb.net split last part of the string - vb.net

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

Related

Using Split in Vb.net in last dash

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.

how to get the fix substring from dynamic string content?

I am developing VB.NET windows app. in VS 2010.
I want to get the substring
$CostCenterId|4^10
from the below string .
PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian
Rupee^$CostCenterId|4^10$LedgerId|2^3$
The position of current string ($CostCenterId|4^10) in the sequence may be change.
but it will always between the two $ sign.
I have written the below code, but confused abt what to write next ?
Public Sub GetSubstringData()
dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian
Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Dim CostIndex As Integer
CostIndex = sDiscription.IndexOf("CostCenterId")
sDiscription.Substring(CostIndex,
End Sub
Have a look into the Split function of a string. This allows you to split a string into substrings based on a specified delimiting character.
You can then do this:
Dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Debug.WriteLine("$" + sfullString.Split("$"c)(3))
Result: $CostCenterId|4^10
You will probably want to do some error checking to make sure the string actually contains the data you expect though.
However looking at the data, what you have is a string containing key-value pairs so you would be better to have a property to hold the CostCenterId and extract the data like this:
Public Property CostCenterId As String
Public Sub Decode(ByVal code As String)
For Each pair As String In code.Split("$"c)
If pair.Length > 0 AndAlso pair.Contains("|") Then
Dim key As String = pair.Split("|"c)(0)
Dim value As String = pair.Split("|"c)(1)
Select Case key
Case "CostCenterId"
Me.CostCenterId = value
End Select
End If
Next
End Sub
Then call it like this:
Decode("PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$")
Why not split() the string by $ into an array, and then look for the element which contains CostCenterId
This should work:
Dim token = "$CostCenterId"
Dim costIndexStart As Integer = sfullString.IndexOf(token)
Dim costIndexEnd As Integer = sfullString.IndexOf("$", costIndexStart + token.Length)
Dim cost As String = sfullString.Substring(costIndexStart, costIndexEnd - costIndexStart + 1)
Result: "$CostCenterId|4^10$"
If you want to omit the dollar-signs:
Substring(costIndexStart + 1, costIndexEnd - costIndexStart - 1)
Try something like this:
Dim CostIndex As Integer
CostIndex = sDiscription.IndexOf("CostCenterId")
auxNum = sDiscription.IndexOf("$"c, CostIndex) - CostIndex
sResult = sDiscription.SubString(CostIndex, auxNum)
Your string,
Dim xString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Substring process,
xString = xString.Substring(xString.IndexOf("$CostCenter"), xString.IndexOf("$", xString.IndexOf("$CostCenter") + 1) - xString.IndexOf("$CostCenter"))
Try this Code:
Dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian" _
& "Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Dim sp() As String = {"$"}
Dim ar() As String = sfullString.Split(sp, StringSplitOptions.RemoveEmptyEntries)
Array.Sort(ar)
MsgBox("$" & ar(0))

How i can get selected parts from string?

How i get string like
'EJ0004','EK0001','EA0001'
from string like
{Emaster.Emp_Code}='EJ0004' OR {Emaster.Emp_Code}='EK0001' OR {Emaster.Emp_Code}='EA0001'
in VB.NET?
You can use a regular expression.
Example:
Sub Main
Dim s = "{Emaster.Emp_Code}='EJ0004' OR {Emaster.Emp_Code}='EK0001' OR {Emaster.Emp_Code}='EA0001'"
Dim pattern = "('\w*')"
Dim matches = Regex.Matches(s, pattern)
Dim values = matches.OfType(Of Match).Select(Function(m) m.Value)
For Each v in values
Console.WriteLine(v)
Next
Console.WriteLine(String.Join(",", values))
End Sub
Output:
'EJ0004'
'EK0001'
'EA0001'
'EJ0004','EK0001','EA0001'
there are many ways you could do this, here i offer one simple way:
dim yourString as string = 'This is the variable which holds your initial string
dim newString as string = yourString.replace("{Emaster.Emp_Code}=", "").replace(" OR ",",")
Now newString will hold 'EA0001' Or whatever.
If you want it without the '' then do
dim newString as string = yourString.replace("{Emaster.Emp_Code}=", "").replace("'","").replace(" OR ",",")

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 ":"

Left of a character in a string in vb.net

say if I have a string 010451-09F2
How to I get left of - from the above string in vb.net
I want 010451
The left function doesn't allow me to specify seperator character.
Thanks
Given:
Dim strOrig = "010451-09F2"
You can do any of the following:
Dim leftString = strOrig.Substring(0, strOrig.IndexOf("-"))
Or:
Dim leftString = strOrig.Split("-"c)(0) ' Take the first index in the array
Or:
Dim leftString = Left(strOrig, InStr(strOrig, "-"))
' Could also be: Mid(strOrig, 0, InStr(strOrig, "-"))
Dim str As String = "010451-09F2"
Dim leftPart As String = str.Split("-")(0)
Split gives you the left and right parts in a string array. Accessing the first element (index 0) gives you the left part.
Sorry not sure on the vb syntax, but the c# is
string mystring ="010451-09F2";
string whatIwant = mystring.Split('-')[0];
Get the location of the dash first (or do it inline), and use that value for the left. This is old school VBA, but it'll be something like this:
Left(YourStringWithTheDash, InStr(YourStringWithTheDash)-1)
dim s as String = "010451-09F2"
Console.WriteLine(s.Substring(0, s.IndexOf("-")))
Console.WriteLine(s.Split("-")(0))
Use something like this:
Mid("010451-09F2",1,InStr("-"))
Dim sValue As String = "010451-09F2"
Debug.WriteLine(sValue.Substring(0, sValue.IndexOf("-"c)))
This helped
Dim str1 as string = me#test.com
Dim str As String
str = Strings.Left(str1, str1.LastIndexOf("#"))