I am using vb.net language in an automation software.
I would like to extract a string from an expression but it display "startIndex cannot be larger than length of string." error message for the few codes that I have used.
The expression is as follow:
Name: Lucas Lion
Age: 48
TaxID: R935279
Nationality: Germany
- First I need to assign the above expression as A, because it is a block of text together.
- The item that I would like to get is the value of the TaxID, that is R935279.
- I assign the value of the TaxID as Result.
Those codes that I have used is as follow:
Result = A.IndexOf("TaxID: ").TrimStart("TaxID: ").Split(Environment.NewLine)(2)
Result = A.Split(cchar(Environment.NewLine))(2).Split(cchar(":"))(1)
Result = A.Substring(ClientInformation.IndexOf("TaxID: ")+"TaxID: ".Length).Split(Environment.NewLine.ToCharArray)(2)
Result = A.Substring(ClientInformation.IndexOf("TaxID:")+"TaxID:".Length).Split(Environment.NewLine.ToCharArr ay)(2)
Could you suggest me where I can improve on the run to get the value of the TaxID?
Re: Problem with extract string from an expression
Hi Lucasi,
Sadly we need some further information before we can help.
Where in your code is:
Name: Lucas Lion
Age: 48
TaxID: R935279
Nationality: Germany
Is this input from elsewhere ?
Is it four separate inputs ?
Is it all in one line ?
Name: Lucas Lion Age: 48 TaxID: R935279 Nationality: Germany
Is it separated with, for example, commas ?
Name: Lucas Lion, Age: 48, TaxID: R935279, Nationality: Germany
If you can show us what you've done already, that would help.
To show us your code:
1. From a new line, paste your code in the Reply page.
2. Select the whole of your code (Highlight it in the usual way).
3. Click the # icon in the tool strip above your page.
This will put 'CODE' and '/CODE' in square brackets around your code.
Poppa
Along with the sunshine there has to be a little rain sometime.
Re: Problem with extract string from an expression
Debug your code. Set one or more breakpoints and step through the code as required. When the exception is thrown, what is the value for startIndex and what is the length of the String? Once you know that, you can work backwards to see where the incorrect index came from. You generally don't fix issues simply by reading the code, because the behaviour of the code depends on the data in use at run time. That's why your integrated development environment (IDE) has a debugger integrated into it. You need to use it.
Re: Problem with extract string from an expression
Originally Posted by Poppa Mintin
Hi Lucasi,
Sadly we need some further information before we can help.
Where in your code is:
Is this input from elsewhere ?
Is it four separate inputs ?
Is it all in one line ?
Is it separated with, for example, commas ?
If you can show us what you've done already, that would help.
To show us your code:
1. From a new line, paste your code in the Reply page.
2. Select the whole of your code (Highlight it in the usual way).
3. Click the # icon in the tool strip above your page.
This will put 'CODE' and '/CODE' in square brackets around your code.
Poppa
Originally Posted by jmcilhinney
Debug your code. Set one or more breakpoints and step through the code as required. When the exception is thrown, what is the value for startIndex and what is the length of the String? Once you know that, you can work backwards to see where the incorrect index came from. You generally don't fix issues simply by reading the code, because the behaviour of the code depends on the data in use at run time. That's why your integrated development environment (IDE) has a debugger integrated into it. You need to use it.
Hi to both of you.
I am creating an example in my initial thread.
Here is how the software looks like. It is a workflow so we need to use vb.net language to process it and the aim in this workflow is to extract from the expression to get the desire string. In my actual workflow I want to extract the TaxID value from the TaxID from, that you can see in the first square box, that is similar to my example. Attachment 175653Attachment 175653
Like I said in my first thread. I have assign the information in the first square box as a variable, in my case is ClientInformation. It does not have comma and not join together. The expression is listed by row.
I am not use I do not have permission to post the second picture in this post and I will continue in the follow post.
Re: Problem with extract string from an expression
Originally Posted by Poppa Mintin
Hi Lucasi,
Sadly we need some further information before we can help.
Where in your code is:
Is this input from elsewhere ?
Is it four separate inputs ?
Is it all in one line ?
Is it separated with, for example, commas ?
If you can show us what you've done already, that would help.
To show us your code:
1. From a new line, paste your code in the Reply page.
2. Select the whole of your code (Highlight it in the usual way).
3. Click the # icon in the tool strip above your page.
This will put 'CODE' and '/CODE' in square brackets around your code.
Poppa
Originally Posted by jmcilhinney
Debug your code. Set one or more breakpoints and step through the code as required. When the exception is thrown, what is the value for startIndex and what is the length of the String? Once you know that, you can work backwards to see where the incorrect index came from. You generally don't fix issues simply by reading the code, because the behaviour of the code depends on the data in use at run time. That's why your integrated development environment (IDE) has a debugger integrated into it. You need to use it.
Hi to both of you.
I am creating an example in my initial thread.
Here is how the software looks like. It is a workflow so we need to use vb.net language to process it and the aim in this workflow is to extract from the expression to get the desire string. In my actual workflow I want to extract the TaxID value from the TaxID from, that you can see in the first square box, that is similar to my example. Attachment 175653Attachment 175653
Like I said in my first thread. I have assign the information in the first square box as a variable, in my case is ClientInformation. It does not have comma and not join together. The expression is listed by row.
Then in the second box, I have assigned the extracted TaxID value as TaxID. My 2 vb.net codes below show similar result when they extract the TaxID value:
Re: Problem with extract string from an expression
Sorry,
Please ignore my last 2 posts.
Here is the correct one.
I am creating an example in my initial thread.
Here is how the software looks like. It is a workflow so we need to use vb.net language to process it and the aim in this workflow is to extract from the expression to get the desire string. In my actual workflow I want to extract the TaxID value from the TaxID from, that you can see in the first square box, that is similar to my example.
The original information from the workflow is from the website called ACME. The original information is like this:
Like I said in my first thread. I have assign the information in the first square box as a variable, in my case is ClientInformation. It does not have comma and not join together. The expression is listed by row.
Then in the second box, I have assigned the extracted TaxID value as TaxID. The vb.net codes below have been used to extract the TaxID value below.
The third box is to print out the value of the extracted TaxID.
However, all of them showing the same result, that is
The startIndex show refer to the second box which is TaxID, the length of the TaxID is fixed with 8 digits (the starting 2 digit is an alphabet and the rest is number)
I hope master can advise on the error of the vb.net syntax
Re: Problem with extract string from an expression
Sorry,
Please ignore my last 2 posts.
Here is the correct one.
I am creating an example in my initial thread.
Here is how the software looks like. It is a workflow so we need to use vb.net language to process it and the aim in this workflow is to extract from the expression to get the desire string. In my actual workflow I want to extract the TaxID value from the TaxID from, that you can see in the first square box, that is similar to my example.
The original information from the workflow is from the website called ACME. The original information is like this:
Like I said in my first thread. I have assign the information in the first square box as a variable, in my case is ClientInformation. It does not have comma and not join together. The expression is listed by row.
Then in the second box, I have assigned the extracted TaxID value as TaxID. The vb.net codes below have been used to extract the TaxID value below.
The third box is to print out the value of the extracted TaxID.
However, all of them showing the same result, that is
The startIndex show refer to the second box which is TaxID, the length of the TaxID is fixed with 8 digits (the starting 2 digit is an alphabet and the rest is number)
I hope master can advise on the error of the vb.net syntax.
I don't think anybody is going to Register to solve your Problem
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
Re: Problem with extract string from an expression
Have you considered regular expressions ?
Code:
Dim lines As String =
"Name: Lucas Lion" & ControlChars.CrLf &
"Age: 48" & ControlChars.CrLf &
"TaxID: R935279" & ControlChars.CrLf &
"Nationality: Germany"
Dim propertyNames = New String() {"Name", "Country", "Age", "TaxID", "Nationality"}
For Each element As String In propertyNames
Dim match = Regex.Match(lines, "(?<=" & element & "\s*:\s*)\w+")
Console.WriteLine(If(match.Success, $"{element,-12} = {match.Value}", $"{element,-12} = not found"))
Next
Results
Name = Lucas
Country = not found
Age = 48
TaxID = R935279
Nationality = Germany
Re: Problem with extract string from an expression
Originally Posted by kareninstructor
Have you considered regular expressions ?
Code:
Dim lines As String =
"Name: Lucas Lion" & ControlChars.CrLf &
"Age: 48" & ControlChars.CrLf &
"TaxID: R935279" & ControlChars.CrLf &
"Nationality: Germany"
Dim propertyNames = New String() {"Name", "Country", "Age", "TaxID", "Nationality"}
For Each element As String In propertyNames
Dim match = Regex.Match(lines, "(?<=" & element & "\s*:\s*)\w+")
Console.WriteLine(If(match.Success, $"{element,-12} = {match.Value}", $"{element,-12} = not found"))
Next
Results
Name = Lucas
Country = not found
Age = 48
TaxID = R935279
Nationality = Germany
Thank you kareninstructor for your reply.
the code need to be in 1 line to fit in the selector expression box which is the assign box if refer to the picture in my last thread. Sorry about the double posting.
In the selector expression, I do not need to define the variable as dimension as we need the vb.net code to extract the TaxID value only.
Re: Problem with extract string from an expression
Originally Posted by lucasi
Thank you kareninstructor for your reply.
the code need to be in 1 line to fit in the selector expression box which is the assign box if refer to the picture in my last thread. Sorry about the double posting.
In the selector expression, I do not need to define the variable as dimension as we need the vb.net code to extract the TaxID value only.
1. your image is unreadable as presented.
2. what I provided works with and without line breaks.
Code:
Dim lines = "Name: Lucas Lion Age: 48 TaxID: R935279 Nationality: Germany"
Dim match = Regex.Match(lines, "(?<=TaxID\s*:\s*)\w+")
If match.Success Then
Console.WriteLine(match.Value)
Else
Console.WriteLine("Failed to get Tax id")
End If