Split string into 3 sections

Hi guys,

I have a pdf file named “Others-Contract-20230731” and I split it in 3 section using CurrentFile.Name.Split(“-”). I’m getting First and Last string (In this case, it means “Others” and “20230731”). But how do I get the middle string (“Contract”)?

If there is anyone who knows the solution, please help me.

1 Like

Hi @balt-ochir.g

CurrentFile.Name.Split(“-”)(1)

When we use split function the output will be stored as a collection. We can use the index to get the value what we want.

Check the below one for better understanding.

Hope it helps!!

2 Likes

Hi @balt-ochir.g

Input.split("-")(1)

1 Like

@balt-ochir.g
StringVar.Split(“-”.ToCharArray)(0) of 0th index
StringVar.Split(“-”.ToCharArray)(1)of 1st index
StringVar.Split(“-”.ToCharArray)(2)of 2nd index

1 Like

Hi @balt-ochir.g

Output:
image

Hope it helps!!

1 Like

Hi @balt-ochir.g

// Assuming CurrentFile.Name contains the filename “Others-Contract-20230731”
// Split the filename using “-” as the separator
splitResult As String() = CurrentFile.Name.Split("-"c)

// Access the middle string (“Contract”) by using the second element (index 1) in the array
middleString As String = splitResult(1)

// Now the “middleString” variable will hold the value “Contract”

Hope it helps!!

1 Like

Hey @balt-ochir.g ,

You can assign a string array variable and in left hand side of assign activity type

StrArray = yourfilename.split(“-”)

then you can use strArray(1) = u will get Contract

1 Like

Assuming you have stored the file name in a variable called fileName, you can use the Assign activity to get the middle string:

  1. Add an Assign activity.
  2. Set the left side of the Assign activity to a new variable, let’s call it middleString.
  3. Set the right side of the Assign activity to fileName.Split("-")(1).
  4. Now, the middleString variable will contain the middle string “Contract”.
1 Like

@balt-ochir.g

System.Text.RegularExpressions.Regex.Matches(Your input string,"([A-Za-z]+|\d+)")

Hope it works!!

It works! Thank you very much.

1 Like

Thank you @balt-ochir.g

Happy Automation!!

Thank you very much.

1 Like

Another way to make it! Thank you very much.

Hi, I tried your solution yesterday and it works. Unfortunately I forgot that I have different type of files. Some files are named “Job Description-20230731” and some are “Others-Contract-20230731” as I mentioned. But when I try to get string with CurrentFile.Name.Split(“-”)(1), it gives me correct string when the file is named “Others-Contract-20230731” but when the file is named “Job Description-20230731” it gives me wrong string (20230731). I want to get first string if there is not any middle string? What should I do? Please help me.

@balt-ochir.g

System.Text.RegularExpressions.Regex.Matches(Your input string,"([A-Z\s*a-z]+|\d+)")

try with the regex you will get your output as required.

Regards

I tried this but it gives me:


Screenshot (5)

@balt-ochir.g

use for each and pass the variable into that and with for each try priting the data



What ever the file might be it will print the data as per your requirement.

Hope it helps!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.