Finding an index of the string

Hi, I have a string

“abcd
efgh
ijkl
mnop
qrst”

This string can be split by the Environment.NewLine but I want to get index of string “mnop”, because the order of this string could change.
for example,
“abcd
mnop
efgh
ijkl
qrst”

For string with Linebreaks
strTest.IndexOf(“SearchString”)

when string is spit eg into an string array
arrSplits.ToList.FindIndex(Function (x) x.Equals("SearchString"))
and will return array item index

It is actually returning -1.

I split the string using

arrSplit = strExtractedList.Split(Environment.NewLine)
intIndex=arrSplit.ToList.FindIndex(Function (x) x.Equals(“ijkl”))

-1 means not present. just share wit us the details by using the immediate panel

also have a look here:

grafik

There has been Spaces post it.

for example - intIndex=arrSplit.ToList.FirstIndex(Function (x) x.Equals("ijkl "))
but can we add this string without space while adding to List itself here

arrSplit = strExtractedList.Split(Environment.NewLine)

we are not sure if we got you but we can harmonize and make it more cleansed by

grafik

@quick_123

See the screenshot. Now, check for the index. It should work.

it still returns space and newline

@quick_123 this is on C#, I hope this will solved your issue if you need the UiPath Code let me know but I know you can write this in UiPath

I’d be glad to help you with finding the index of the string “mnop” in UiPath, even when the order changes within a multi-line string:

Method 1: Using String.IndexOf (Efficient for Newer UiPath )

This method leverages the built-in String.IndexOf function, which offers efficiency if you’re using a compatible UiPath version:

string inputString = @"abcd
efgh
ijkl
mnop
qrst";

// Find the index of "mnop" (case-sensitive)
int index = inputString.IndexOf("mnop");
inputString.Split(Environment.NewLine.ToCharArray());
if (index >= 0)
{
    // "mnop" found at index: {index}
    Console.WriteLine($"mnop found at index: {index}");
}
else
{
    // "mnop" not found
    Console.WriteLine("mnop not found in the string");
}

Method 2: Splitting by New Lines and Searching the Array (Works for All Versions)

This method is applicable for any UiPath version and involves splitting the string by newlines and then searching the resulting array for “mnop”:

string inputString = @"abcd
efgh
ijkl
mnop
qrst";

// Split the string into lines (assuming Environment.NewLine is the correct newline character)
string[] lines = inputString.Split(Environment.NewLine.ToCharArray());

// Find the index of "mnop" (case-sensitive)
int index = Array.IndexOf(lines, "mnop");

if (index >= 0)
{
    // "mnop" found at index: {index}
    Console.WriteLine($"mnop found at index: {index}");
}
else
{
    // "mnop" not found
    Console.WriteLine("mnop not found in the string");
}

Explanation:

  • Both methods first define the multi-line string.
  • Method 1:
    • String.IndexOf takes the target string (“mnop”) as an argument and returns its starting index within the inputString if found, or -1 otherwise.
    • An if statement checks if the index is greater than or equal to 0 (meaning “mnop” was found).
  • Method 2:
    • Split divides the string into an array of individual lines based on the newline character (Environment.NewLine).
    • Array.IndexOf searches the lines array for “mnop” and returns its index within the array if present, or -1 if not found.
    • Similar logic is used in the if statement to handle success or failure.

Choosing the Right Method:

  • Method 1 is generally more efficient for newer UiPath as it avoids creating an intermediate array.
  • Method 2 is a versatile option if you’re unsure of your UiPath version or need to handle more complex string manipulation scenarios.

Remember to replace the placeholder string ("abcd\nefgh\nijkl\nmnop\nqrst") with your actual multi-line string within your UiPath workflow.

The string also has space in them and also they separated by new line.

mimicked data for your reference.

APPLE IS FRUIT
ORANGE/ORANGE FRUIT
MANGO MANGO IS FRUIT
GRAPES IS FRUIT
AVACADO FRUIT
PLUM IS FRUIT
BLUEBERRY
PASSIONFRUIT
TUNA
HONEY BEE
FRUIT/JUICE SNACKS
LIKE ABCD EFGH
from this string PASSION FRUIT is the string i need index of

Hi @quick_123

Assign activity -> inputString = "APPLE IS FRUIT
                                  ORANGE/ORANGE FRUIT
                                  MANGO MANGO IS FRUIT
                                  GRAPES IS FRUIT
                                  AVACADO FRUIT
                                  PLUM IS FRUIT
                                  BLUEBERRY
                                  PASSIONFRUIT
                                  TUNA
                                  HONEY BEE
                                  FRUIT/JUICE SNACKS
                                  LIKE ABCD EFGH"

Assign activity -> index = Array.IndexOf(inputString.Split(Environment.NewLine.ToCharArray()), inputString.Split(Environment.NewLine.ToCharArray()).Where(Function(x) x.Contains("PASSIONFRUIT")).FirstOrDefault())

Message Box -> index

index is of DataType System.Int32

Regards