VBA - Split from right to left (REVERSED) - vba

I want to extract the last folder from a folder path:
path = C:\Users\z204685\tecware\RESULTS\D1369_3 (R=0) Surface Failure
I want to extract the last part, after the "\", in a new string:
newString = "D1369_3 (R=0) Surface Failure"
Maybe reversing the path string, then using the Split function with "\" and then reversing it again...Any better ideas?

Here's my stab at it:
Sub test()
Dim testString As String
Dim test As Long
Dim output As String
testString = "C:\Users\z204685\tecware\RESULTS\D1369_3 (R=0) Surface Failure"
test = InStrRev(testString, "\")
output = Right(testString, Len(testString) - test)
End Sub
Hope it gets you close!

Look up the InStrRev function.

Another solution, according to #TimWilliams comment, You can achieve this using Split and UBound
Sub test()
Dim fpath As String
Dim newString As String
Dim temp_arr As Variant
fpath = "C:\Users\z204685\tecware\RESULTS\D1369_3 (R=0) Surface Failure"
temp_arr = Split(fpath, "\")
newString = temp_arr(UBound(temp_arr))
End Sub

Related

Need to strip numbers out of a filename string to change save location

I'm having an issue where I need to strip a filename from a path but can't quite figure out the code.
An example filename would be C:\Checked out parts\001-1099-01.slddrw. I need to extract the "001-1099-01." portion. The file location to the left could be anything and the only constants in the file name are the "001-" portion (which I should point out, could repeat if the filename was 001-1001-03) and the ".slddrw". Other than that the filename could be named "001-10999-03-02-01".
I have stripped out the slddrw portion easily, I tried using Right and InStr functions to strip the rest off but I think that InStr only works with letters (couldn't find any number examples anyways)
I believe this is what you are looking for:
Public Sub test()
Dim strFileName As String
'Your sample file name
strFileName = "C:\Checked out parts\001-1099-01.slddrw"
'Search for the first occurance of \ in the reversed (!) file name
' if you substract this result from the length of the string
' you know where to start (+ 2 to avoid the \ itself)
strFileName = Mid(strFileName, Len(strFileName) - InStr(1, StrReverse(strFileName), "\") + 2)
'Remove the .slddrw portion from the end
strFileName = Replace(strFileName, ".slddrw", "")
'Done
Debug.Print strFileName
End Sub
Note the comments in the code for more information.
You can use a sub in which you define the path and extension of your file and it will give you the filename.
Sub getFileName()
Dim myString() As String
Dim path As String, extension As String
Dim fileName As String
path = "C:\Checked out parts\"
extension = ".slddrw"
'Say your string is in cell A1
myString = Split(Range("A1"), path)
fileName = Split(myString(1), extension)(0)
MsgBox fileName 'fileName can be used elsewhere after
End Sub
You could also do it as a function:
Function getFileName(cel As Range, path As String, extension As String)
Dim myString() As String
myString = Split(cel, path)
getFileName = Split(myString(1), extension)(0)
End Function
If you have the path and extensions stored in cells, you can use this function:
Function getFileName2(cel As Range, path As Range, extension As Range)
Dim myString() As String
myString = Split(cel, path)
getFileName2 = Split(myString(1), extension)(0)
End Function
Those are the options I can think of, hope it helps.
EDIT:
If you don't know the path nor the extension, you can use this instead:
myString = Split(Range("A1"), "\")
fileName = Split(myString(UBound(myString)), ".")(0)

vb.net I want to break the words in my string

My string is myname mynickname.
I want to get 2 word in my string myname and mynickname.
How to do in "vb.net"
You should use the String.Split method:
Sub Sample()
Dim theStringIWantToSplit As String = "myname mynickname"
'' if there is a possiblility of multiple spaces between the words and you want to ignore them
Dim sa() As String = theStringIWantToSplit.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
'' otherwise it is much simpler
Dim sa1() As String = theStringIWantToSplit.Split(" "c)
End Sub
dim xstr as string="myname mynickname"
dim xMyname as string=xstr.Split(" ",
options:=StringSplitOptions.RemoveEmptyEntries)(0)
dim Name as string=xstr.Split(" ",
options:=StringSplitOptions.RemoveEmptyEntries)(1)

How to find a phrase in a string based on a character

Let's say I have the following string:
sdfhahsdfu^asdhfhasdf^asd7f8asdfh^asdfhasdf^testemail#email.com^asdhfausdf^asodfuasdufh^alsdfhasdh
What's the best way of extracting the email from that string? I thought of maybe split(string, "#") but then I'm not sure where to go from there.
Note: the email will always be flanked by ^ on either side, but the position in the string will be different depending on the string.
You can use Regex to find your string. Try something like:
System.Text.RegularExpressions.Regex.Match("\^[^\^]+#[^\^]+\^", myString)
I would split over ^ and then loop through all items to find something containing a #
'1 form with:
' 1 command button: name=Command1
Option Explicit
Private Sub Command1_Click()
Dim lngItem As Long
Dim strString As String
Dim strItem() As String
strString = "sdfhahsdfu^asdhfhasdf^asd7f8asdfh^asdfhasdf^testemail#email.com^asdhfausdf^asodfuasdufh^alsdfhasdh"
strItem = Split(strString, "^")
For lngItem = 0 To UBound(strItem)
If InStr(strItem(lngItem), "#") > 0 Then
DoEmail strItem(lngItem)
End If
Next lngItem
End Sub
Private Sub DoEmail(strEmail As String)
Print strEmail
End Sub

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 ",",")