How to get values between two Topic?

Hi,

May I know how to get the values that are between two Topic?

Eg.

Topic 1

Text 1
Text 2
Text 3

Topic 2

Topic 3

How to get the values between Topic 1 and Topic 2 ?
And after that, get the values between Topic 2 and Topic 3

I want to get the values between Topic 1 and Topic 2, before it goes to Topic 2 area

Thank you

Assuming you have the values in a List

Using IndexOf and GetRange methods you can extract the values.

indexOfTopic1 = ListStr.IndexOf("Topic 1")
indexOfTopic2 = ListStr.IndexOf("Topic 2")
indexOfTopic3 = ListStr.IndexOf("Topic 3")
result1 = ListStr.GetRange(indexOfTopic1+1,indexOfTopic2-(indexOfTopic1+1))
result2 = ListStr.GetRange(indexOfTopic2+1,indexOfTopic3-(indexOfTopic2+1))

Always check if indexes are greater than 0.
If any element is not found in the list, index will be -1.

Regards,
Karthik Byggari

Hi @KarthikByggari,

What if the values is not in a List ?

The IndexOf is it mean that it will start from that string, so the GetRange get values between the two string ?

Thank you

You can convert to List.
For example, if values are in an array, you can convert to List

list = new List<int>(intArray)
or
list = array.ToList

IndexOf returns the index of the value found in a list.
Get Range gets the list of values from the given index and number of elements.
GetRange(intIndex, intNumberOfElements)

For Example,

list contains the following values -

Topic 1
Text 1
Text 2
Text 3
Topic 2
Text 1
Text 2
Text 3
Topic 3

IndexOf(“Topic 1”) - returns 0
IndexOf(“Topic 2”) - returns 4
IndexOf(“Topic 3”) - returns 8

GetRange(IndexOf(“Topic 1”) +1, 4 - (IndexOf(“Topic 1”)+1)) - returns the values from index 1 to index 3.

Result:
Text 1
Text 2
Text 3

Regards,
Karthik Byggari

1 Like

Hi @KarthikByggari,

I am using Word Documents

So to get the values, I have to put for example (Topic 1 to Topic 3) into a list ?

Will it works if I did not put it into a list ?

Thank you

After getting the values in a string, split the string to array and convert to list.
The above code won’t work if it is not in a list.

Please follow the below approach if your values are in a string (using string manipulations).

indexOfTopic1 = str.IndexOf("Topic 1")
indexOfTopic2 = str.IndexOf("Topic 2")
indexOfTopic3 = str.IndexOf("Topic 3")

result1 = str.SubString(indexOfTopic1 - "Topic 1".Length + 1, indexOfTopic2 -1)
result2 = str.SubString(indexOfTopic2 - "Topic 2".Length + 1, indexOfTopic3 -1)

Hi @KarthikByggari,

Thank you for the explanation! :slight_smile:

I will try the approach and get back to you

Thank you!

1 Like

Hi @KarthikByggari,

I uses Matches activity to get all string that has the word ‘Topic’ at the front
I put the Matches result into a ForEach loop
Thus now if you run the workflow, the item will be - Topic 1, Topic 2, …

Next I uses the Assign activity
I tried to get the Index of each item

option

When I uses MessageBox to see the result of indexOfTopic1, the result is 0

The result is it suppose to be 0 ?

Thank you

String indices starts from 0.
So if your string is “Topic 1 something”, and you look for the index of Topic 1, it returns the first character index where it found.
So in this case 0.

The answer for your question is, the result is 0.

Hi @KarthikByggari,

May I ask if the following Assign activity will be able to get the index for all Topic ?
As I only able to get 0

option

Thank you

Hi @KarthikByggari,

I tried the method, however it show the following error

index

Do you know why it will have this error ?

Thank you

YourString.IndexOf(findstring) returns -1 if the string which you are looking is not found.
If found, then index will greater than or equal to 0.

When using string.substring , the indices will be greater than or equal to 0.

Hi @KarthikByggari,

Thanks for your reply!

I will try again and get back to you again

Thank you!

Hi @KarthikByggari,

I have tried using this solution. I have converted my array into a list. I did get the text between Topic 1 and Topic 2.
However, these codes are static and need to manually change it.

I was thinking of getting all topics into a list and replace the string in the bracket to the list item.

However, how do I make it dynamic like it will take the text between Topic 1 and 2, and then move on to getting the text between Topic 2 and 3?

Is this possible?

Thank you

Are the number of topics fixed and changes everytime?

Hi @KarthikByggari,

For this Word Document, the number of topics are fixed.
However, if we change to another Word Document, the number of topics may change

I was thinking of getting all topics from this document and then let it get the text from Topic 1 and 2, and then get the text from Topic 2 and 3 dynamically

But I do not know how I can let the program go to Topic 1 and 2, then Topic 2 and 3, and so on.

Thank you

Hi @KarthikByggari,

Have you thought of any methods or ways that can allow the program go to Topic 1 and 2, then Topic 2 and Topic 3, and so on?

Thank you

First you have to find the count of number of topics available.

Like below -

Create a variable of integer type , intCount = 0
Create a variable of integer type, intIndex = 1
Create a variable of boolean type, boolFound = True

Use do while loop activity -

do
{
    strTopic = strTopic + intIndex.ToString
    if(wordstr.Contains(strTopic))
    {
       intCount = intCount + 1
       intIndex = intIndex + 1
    }
    else
    {
       boolFound = False
     }
    condition
   boolFound == True
}

The number of topics will be in document will be intCount

Use For Each Loop Activity from i=1 to intCount

Regards,
Karthik Byggari

Hi @KarthikByggari,

Thanks for your reply!

So to get the text in between every two Topic, I have to use intCount in For Each loop?

These are what my current codes look like

option_1


option_3

Thank you

1 Like

That’s perfect. :slight_smile: