I want to split a string in vbscript language.
The string contains : shipping costs 10%
I want to get the value before '%' but only the number.
I try with this but it doesn't work
spese = Split(spese,'%')(0)
Your code to retrieve everything before the % doesn't compile because you use conment signs as delimiter (') and not (")
After fixing that you also would have to cut out the value itself.
Regarding to the new conditions here is the solution in vb.net, assuming before the number there always is a blank:
Dim myString As String
myString = "shipping costs 10%"
Dim tmp As String
tmp = Left(myString, InStr(myString, "%") - 1)
Dim result As String
result = Right(tmp, Len(tmp) - InStrRev(tmp, " "))
The solution of #AndrewMorton (see comments of your question) is much more elegant. You should use it.
Related
I have a bunch of different sets of engineering measurements in the format:
77.170 (+/- 0.025)
And I need to split it into the first number, which is the nominal value, and the number in the parenthesis, the tolerance. Not sure exactly how to do this in excel VBA. I was thinking I would use the Split function with a space delimiter, giving me the first number, then the unnecessary characters, then the tolerance, but the tolerance will include a parenthesis. How could I get rid of just that parenthesis, and will what I just suggested even work? Thanks!
Consider:
Sub dural()
s = "77.170 (+/- 0.025)"
s2 = Replace(Replace(Replace(s, " ", ""), "+/-", ""), ")", "")
ary = Split(s2, "(")
MsgBox ary(0) & vbCrLf & ary(1)
End Sub
Use Text to Columns and a formula.
Go to Data--->Text to Columns. Choose delimited and choose Space as your delimiter. This should split the text string into something like:
ColA |ColB|ColC
77.170|(+/-|0.025)
Column C is a bit funky, so let's just grab everything but the last character.
In column D put this:
=LEFT(C1,LEN(C1)-1)
Finally, you should get:
ColA |ColB |ColC |ColD |
77.170|(+/- |0.025)|0.025|
I would use a combination of instr() and mid to get what you need. For example
measurments="77.170 (+/- 0.025)"
mid(measurements,1,instr(measurements," "))
trim(mid(measurements,instr(measurements,"-")+1,instr(measurements,")")-instr(measurements,"-")-1))
or, to combine,
measurments="77.170 (+/- 0.025)"
mid(measurements,1,instr(measurements," ")) & " " & trim(mid(measurements,instr(measurements,"-")+1,instr(measurements,")")-instr(measurements,"-")-1))
Try using a combination of InStr(), Left(), Right().
Get the index/position of the '(' using InStr and then extract the characters using Left and Right. If you want to get the final data as a double or a Long use CDbl() or CLng() respectively.
For getting text out of other text consider using Regular Expresions.
To use them in VBA you will need in Reference 'VBScript_RegExp_55' library.
The reason why you might want to do that is because following code returns whatever first two numbers show up in your text( it can be modified to be much smarter than that), regardless of other text around it.
Dim Regex As RegExp
Dim Matches As MatchCollection
Set Regex = New RegExp
Regex.Pattern = "\d*\.\d*"
Regex.Global = True
Set Matches = Regex.Execute("77.170 (+/- 0.025)")
MsgBox (Matches(0).Value & " " & Matches(1).Value)
Assuming s is your measurement string, here is the most direct way:
v = Split(Left(s, Len(s) - 1), " (+/- ")
That's it. Now v(0) holds the nominal value and v(1) holds the tolerance.
Perhaps this is a simple solution for most, but I can't get this to work like it should according to syntax.
I have this line of text "Part Number123456Price$50.00"
I want to pull the part number out of it, so I use this function...
str = Mid(str, str.IndexOf("Part Number") + 12, str.IndexOf("Price"))
My results are str = "123456Price$50.0" every time. I know the part number can vary in length so I need a solid solution of pulling this out.
It can be confusing to mix the legacy VB string methods (such as Mid) with the .Net string methods (like IndexOf). The VB methods use 1 as the index of the first character while the .Net methods use 0.
The following code will extract the part number from a string
Dim str As String = "Part Number123456Price$50.00"
Dim iPart As Integer = str.IndexOf("Part Number") + 11
Dim iPrice As Integer = str.IndexOf("Price")
str = str.Substring(iPart, iPrice - iPart).Trim
The Mid() function of Visual Basic is documented as having three arguments: (1) a string, (2) the beginning location in the string, and (3) the number of characters to copy.
So if your string is "Part Number123456Price$50.00" and you want to pull the part number as a series of digits, the "123456" part of the string, using the Mid() function then you need to find the beginning of the part number digit string and to then know the number of digits.
If your string is in the variable str then you can find the offset by something like str.IndexOf("Number") + len("Number") which will provide the offset to after the string "Number".
Next you need to find the number of digits so you would do something like str.IndexOf("Price") to find where the text "Price" begins and then subtract from that offset the offset of where the digits begin.
The result of all of this is you need a bit of code something like the following. I have not tested this source as I am not a VB programmer so it may need a tweak and you might want to put some checks on data validity as well.
Dim TextNumber as String = "Number"
Dim TextPrice as String = "Price"
iOffset = str.IndexOf(TextNumber) + len(TextNumber)
str = Mid(str, iOffset, str.IndexOf(TextPrice) - iOffset)
Alternatively, if Price is always the format $00.00, this will also work.
Dim str as String = "Part Number123456Price$50.00"
str = str.Remove(str.IndexOf("Price"))
I have a string like this adas:asd":"asd:asdas:dasd":"ad33q3sd:asd
I want to split it like
Dim splited() As String
splited = Split(input,"":"")
but its not working i tried adding \ but same
splited = Split(input,"\":\"")
Your Split should look like this:
splited = Split(input, """:""")
The first and last " define the string. The "" escapes each "
This "\":\"" is the C# notation
The other method works well, this is just a variation. vba has a function called chr(ascii_value), you supply the ascii value and chr returns the corresponding character. In this case a quote is ascii 34
splited = Split(input, chr(34) & "." & chr(34))
we've basically concatenated (&) our quotes with the period. using chr() for all your characters may be a bit silly and hard to read, but for quotes I think it's well used
I have a variable like this:
var = "word1 (should be replaced by word1)"
I need to get word1 by itself and sometimes it may be a word(s) but it will always have (*) after it. Any way to delete text between the () signs, then remove the (), then remove any space(s) after the last word?
Not sure of any way to go about it.
The simplest would be:
var = Trim(Mid(var, 1, InStr(var, "(") - 1))
InStr returns the index of the first occurrance of "(". I subtract 1 to discount the position held by the "(".
Mid cuts the string from the first character at position 1, to the index returned by the InStr.
Then Trim to remove trailing spaces.
Like this?
Sub Sample()
Dim sVar As String
Dim MyAr() As String
sVar = "word1 (should be replaced by word1)"
MyAr = Split(sVar, "(")
Debug.Print MyAr(0)
End Sub
EDIT
The above code can be shortened further
Sub Sample()
Dim sVar As String
sVar = "word1 (should be replaced by word1)"
Debug.Print Split(sVar, "(")(0)
End Sub
I would use SPLIT function.
arrayVar2 = split(var, "(" )
You will now have an array with 2 elements.
Split the second element
arrayVar3 = split( arrayVar2[1], ")" )
Then join:
out = arrayVar2[0] & arrayVar3[1]
That will work as long as there is only ever a single set of brackets in the string.
The alternative way is to make use of the regular expression library but that is a longer answer. It wouldn't be as efficient but would be better if needing to handle more complex strings.
UPDATE: I went a bit further than the question perhaps called for. My solution ensures that the bracketed text is removed - any text AFTER the bracketed text is also retained.
I got this url for example "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
I want to get the last digit numbers and the rest could be anything.
I need a format like this "http://www.yellowpages.com/anything.... lid=randomdigitnumbers" or as long as i get those numbers.
My knowledge is very poor in this regex thing so please guys help me.
the following did not work
Dim r As New System.Text.RegularExpressions.Regex("http://www.yellowpages.com/.*lid=d*", RegexOptions.IgnoreCase)
Dim m As Match = r.Match(txt)
If (m.Success) Then
Dim int1 = m.Groups(1)
MsgBox("(" + int1.ToString() + ")" + "")
End If
thank you in advance
Using Regular Expressions for this is a bit of overkill, IMO.
You could accomplish the same thing using string functions:
Dim url As String = "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
Dim queryString As String = url.SubString(url.IndexOf("?"), url.Length - url.IndexOF("?"))
Dim nameValuePairs As String() = queryString.Split("=")
Dim lid As String = nameValuePairs(1)
This is off the top of my head, so you may need to tweak it a bit. The basic concept is to the portion of the URL after the ? (the query string), and then split it on the = sign, taking the second element of the resulting array (the value).
Also, if the query string has more than one name value pair, they'll be separated by &, so you'll need to split on the ampersand (&) first, then the equal signs.
Just find lid= and get everything after that:
Dim url As String = "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
Dim lidIndex As Integer = url.IndexOf("lid=") + "lid=".Length
Dim lid As Integer = url.Substring(lidIndex)