I have a String in VBA with this text: < History Version="1.10" Client="TestClient001" >
I want to get this TestClient001 or anything that's inside Client="xxxx"
I made this code but it's not working
Client = MID(text,FIND("Client=""",text)+1,FIND("""",text)-FIND("Client=""",text)-1)
Is there a way to specifically get the text inside Client="xxxx"?
There's no such function as Find in VBA - that's a worksheet function. The VBA equivalent is InStr, but I don't think you need to use it here.
The best tool for extracting one string from another in VBA is often Split. It takes one string and splits it into an array based on a delimiting string. The best part is that the delimiter doesn't have to be a single character - you can make it an entire string. In this case, we'd probably do well with two nested Split functions.
Client = Split(Split(text,"Client=""")(1),Chr(34))(0)
The inner Split breaks your text string where it finds "Client="". The (1) returns array element 1. Then the outer Split breaks that returned text where it finds a " character, and returns array element 0 as the final result.
For better maintainability, you may want to use constants for your delimiters as well.
Sub EnclosedTextTest()
Const csFlag1 As String = "Client="""
Const csFlag2 As String = """"
Const csSource As String = "< History Version=""1.10"" Client=""TestClient001"" >"
Dim strClient As String
strClient = Split(Split(csSource, csFlag1)(1), csFlag2)(0)
Debug.Print strClient
End Sub
However, if the Split method doesn't work for you, we can use a method similar to the one you were using, with InStr. There are a couple of options here as well.
InStr will return the position in a string that it finds a matching value. Like Split, it can be given an entire string as its delimiter; however, if you use more than one character you need to account for the fact that it will return where it finds the start of that string.
InStr(1,text,"Client=""")
will return 26, the start of the string "Client="" in the text. This is one of the places where it's helpful to have your delimiter stored in a constant.
intStart = InStr(1,text,csFlag1)+len(csFlag1)
This will return the location it finds the start of the delimiter, plus the length of the delimiter, which positions you at the beginning of the text.
If you store this position in a variable, it makes the next part easier as well. You can use that position to run a second InStr and find the next occurrence of the " character.
intEnd = InStr(intStart,text,csFlag2)
With those values, you can perform your mid. You code overall will look something like this:
Sub InstrTextTest()
Const csFlag1 As String = "Client="""
Const csFlag2 As String = """"
Const csSource As String = "< History Version=""1.10"" Client=""TestClient001"" >"
Dim strClient As String
Dim intPos(0 To 1) As Integer
intPos(0) = InStr(1, csSource, csFlag1) + Len(csFlag1)
intPos(1) = InStr(intPos(0), csSource, csFlag2)
strClient = Mid(csSource, intPos(0), intPos(1) - intPos(0))
Debug.Print strClient
End Sub
This will work, but I prefer the Split method for ease of reading and reuse.
You can make use of Split function to split at character = then with last element of the resulting array remove character quotes and > with help of replace function and you will get the required output.
In the end I got it thanks to the idea given by #alok and #Bigben
Dim cl() As String
Dim ClientCode As String
If (InStr(1, temp, "Client=", vbTextCompare) > 0) Then
cl = Split(temp, "=")
ClientCode = cl(UBound(cl))
ClientCode = Replace(ClientCode, """", "")
ClientCode = Replace(ClientCode, ">", "")
It's XML, so you could do this:
Dim sXML As String
sXML = "<History Version=""1.10"" Client=""TestClient001"">"
With CreateObject("MSXML.Domdocument")
.LoadXML Replace(sXML, ">", "/>") 'close the element
Debug.Print .FirstChild.Attributes.getnameditem("Client").Value
End With
I am baffled what is going on here:
Dim sSearchIn As String = "Bla"
Dim sSearchFor As String = Vb6ChrW(65533) 'This results in the string "�"
Dim i As Integer = sSearchIn.IndexOf(sSearchFor, 0)
I expect "i" to be -1 because the string "�" does not exist in the string "Bla".
However, "i" is 0.
What am I missing?
Thank you!
Public Function Vb6ChrW(ByVal uKeyCode As Integer) As String
Dim nChar As Char = Char.ConvertFromUtf32(uKeyCode)
Return New String(nChar, 1)
End Function
You are performing culture-sensitive search. It is behaving as documented:
In a culture-sensitive search, if value contains an ignorable character, the result is equivalent to searching with that character removed. If value consists only of one or more ignorable characters, the IndexOf(String, Int32) method always returns startIndex
If you wanted a char-code-level search, you should have used
sSearchIn.IndexOf(sSearchFor, StringComparison.Ordinal)
, that gives -1 as you expect.
You should also turn Option Strict On so that it correctly brakes your Vb6ChrW function that you don't need in the first place because ChrW exists.
I have one variable which contains integer values in string format as given below:
strValue = "123"
I want to convert this string value to integer type so it will be helpful for mathematical operation.
How can I do that in Javascript(Rhino) Language?
You can use the parseInt function that recieves a string and returns int.
Example:
var string = "123"
var number = parseInt(string)
print(number * 2)
When comparing string to another string using compareTO() method, here we compare one string to another string right. My doubt is which one is argument string in this two
example : int result = str1.compareTo( str2 );
If you write
str1.compareTo(str2)
then you get back an integer that is
negative if str1 precedes str2,
zero if str1 equals str2, and
positive if str1 is comes after str2.
Here, str1 is the receiver object (since it comes before the dot) and str2 is the argument.
I cannot find a solution as how to chop off part of a string(filepath) and use what's left to get another string.
For example: if I have filepath: Q:\2456_blah_blah\file.txt and I want to put this entire filepath into an array and delete from left characters 0 through 8 (which is consistent for my application) so I'd have 'blah_blah\file.txt' left for my array.
(The blah_blah portion is going to be a dynamic length, but it will always be letters.)
So then I'd like to take the 'blah_blah\file.txt' and pull just from that beginning up to the next '\' symbol, getting "blah_blah" as a string. I'm not too savy with VB strings at this point, so any help would be much appreciated. This is just my thinking how it could be done, if there's a simpler solution, I'm all ears.
Thanks,
Two Substring calls should do it.
Dim str As String = "Q:\2456_blah_blah\file.txt"
str = str.Substring(8, str.Length - 8)
Dim blah As String = str.Substring(0, str.IndexOf("\"))
Are you looking for something like this. Not knowing all the details makes it difficult to create something that fits your needs but I'll give it a shot. This will return back the blah_blah portion of your strings no matter how large "blah_blah" is, no matter how long the filename is, no matter how long the first portion of your string is and no matter how deep the path goes.
Dim test As String = "c:\34242_blah_blah\test.txt"
Dim startPos As Integer = test.IndexOf("_") + 1
Dim endPos As Integer = test.IndexOf("\", startPos)
Dim result As String = test.Substring(startPos, endPos - startPos)