How to extract using both substring and split method

Here is 3 rows of text fille–

name - Sagar
location - Bhubaneswar
department - Civil engineer

How to extract only Sagar, Bhubaneswar and Civil engineer . How can i extract using both split and substring method?

Hi @ydash999

Try this below expression

Use Assign Activity
LHS - Name
RHS - “Input String”.Substring(“Input String”.IndexOf("Name - ") + "Name - ".Length).Split(Environment.NewLine.ToCharArray)(0)

Use Assign Activity
LHS - location
RHS - “Input String”.Substring(“Input String”.IndexOf("location - ") + "location - ".Length).Split(Environment.NewLine.ToCharArray)(0)

Use Assign Activity
LHS - department
RHS - “Input String”.Substring(“Input String”.IndexOf("department - ") + "department - ".Length).Split(Environment.NewLine.ToCharArray)(0)

Hop it will works

Regards
Gokul

Alternative method try with regex expression

To get Name - (?<=name -)\s(\S+)

Use Assign Activity
LHS - Create an variable
RHS - System.Text.RegularExpression.Regex.Match(“Input string”,"(?<=name -)\s(\S+)").Tostring.Trim

To get location - (?<=location -)\s(\S+)

Use Assign Activity
LHS - Create an variable
RHS - System.Text.RegularExpression.Regex.Match(“Input string”," (?<=location -)\s(\S+)").Tostring.Trim

To get department - (?<=department -)\s(\S.+)

Use Assign Activity
LHS - Create an variable
RHS - System.Text.RegularExpression.Regex.Match(“Input string”," (?<=department -)\s(\S.+)
").Tostring.Trim

Regards
Gokul

thanks bro!any other method which will be using only split method ? this method is helpful but looks long

You can try with regex method @ydash999

1 Like

split( Str1,"-").lastorDefault.TrimStart

str1 use any of three string
name - Sagar
location - Bhubaneswar
department - Civil engineer

Assuming the data is from a simple multiline textfile (notepad)

name - Sagar
location - Bhubaneswar
department - Civil Engineer

1. Read Text File Activity - Read data and assign to a temporary variable
Output “tempVar” (String)

This lumps everything into a single variable but keeping the format.

2. Use Assign Activity - Split multiline text into String[Array]
“newVarArr” (String[Array]) = Split(tempVar, Envrionment.NewLine)

This splits the strings at newline into different array item.

3. Use For Each Activity
Loop through “newVarArr” and do whatever you need to. In my case I simply write them out in output panel using Write Line Activity.

For Each “item” in “newVarArr”

Write Line: Split(item.ToString, " - ").lastOrDefault

This will again split the string at " - " (with space either side of hyphen) and return only the last string.

Result:
Sagar
Bhubaneswar
Civil Engineer

If you want to assign the result to individual variable NOT USING for each loop. Simply use Assign Activity:

"nameVar" (String) = Split.(newVarArr(0).ToString, " - ").lastOrDefault

Result: nameVar = Sagar

1 Like

yes i want to extract the individual datas from get text file,whose output is of string variable

what is the use of lastor default.trimstart?

Yes in this case first follow step 1 and 2.

The main point here is to first separate each lines, and then manipulate them individually.

At Step 1, since Read Text File activity only output one lump string, if you log the Read Text File output using Log Message Activity, you’ll see they end up exactly the same as in the text file:

name - Sagar
location - Bhubaneswar
department - Civil Engineer

Step 2 is to separate each line and assign them into a String[Array] using Split method, which gives you:

newVarArr = [“name - Sagar”, “location - Bhubaneswar”, “department - Civil Engineer”]

now you can access each element individually using their index.

newVarArr(0) = "name - Sagar"
newVarArr(1) = "location - Bhubaneswar"
newVarArr(2) = "department - Civil Engineer"

Step 3 is basically up to you wherever you want to store the value. Just remember when you assign the value to a new variable you’ll need to manipulate it during assigning process. For Example:

newVariable = newVarArr(1)

The above will assign “location - Bhubaneswar” to newVariable but you only want “Bhubaneswar” so you need to manipulate it by doing this instead:

newVariable = Split(newVarArr(1).ToString, " - ").lastOrDefault

In a nutshell, the argument above will

  1. Split the newVarArr(1) at " - " (include white space) resulting “location” "Bhubaneswar"
  2. the method .lastOrDefault return the last element of a sequence, in this case "Bhubaneswar"

newVariable = "Bhubaneswar"

.TrimStart that @shashank.singh mentioned is a method that removes all white space in front of a string. Since his split( Str1,"-").lastorDefault.TrimStart doesn’t include white space in “-”, the .lastOrDefault will return " Bhubaneswar" as you see it starts with white space. .TrimStart will remove that.

Hope this helps

1 Like

Thankss! Very elaborated n helpful!

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