# How to extract using both substring and split method

**URL:** <https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104>\
**Category:** Studio\
**Tags:** activities-panel, question, studio, system, activities\
**Created:** [February 2, 2022, 8:43am UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104 "2022-02-02T08:43:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ydash999](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ydash999/32/289476_2.png) [@ydash999](https://forum.uipath.com/u/ydash999)\
**Post date:** [February 2, 2022, 8:43am UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/1 "2022-02-02T08:43:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Gokul001](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/gokul001/32/462734_2.png) [@Gokul001](https://forum.uipath.com/u/Gokul001)\
**Post date:** [February 2, 2022, 8:56am UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/2 "2022-02-02T08:56:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gokul001](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/gokul001/32/462734_2.png) [@Gokul001](https://forum.uipath.com/u/Gokul001)\
**Post date:** [February 2, 2022, 9:02am UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/3 "2022-02-02T09:02:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ydash999](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ydash999/32/289476_2.png) [@ydash999](https://forum.uipath.com/u/ydash999)\
**Post date:** [February 2, 2022, 9:08am UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/4 "2022-02-02T09:08:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gokul001](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/gokul001/32/462734_2.png) [@Gokul001](https://forum.uipath.com/u/Gokul001)\
**Post date:** [February 2, 2022, 9:13am UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/5 "2022-02-02T09:13:22Z")

</div>

You can try with regex method @ydash999

---

<div class="post-metadata">

**Author:** ![shashank.singh](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/shashank.singh/32/263451_2.png) [@shashank.singh](https://forum.uipath.com/u/shashank.singh)\
**Post date:** [February 2, 2022, 11:18am UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/6 "2022-02-02T11:18:48Z")

</div>

split( Str1,“-”).lastorDefault.TrimStart

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

---

<div class="post-metadata">

**Author:** ![ecSC2](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ecsc2/32/282242_2.png) [@ecSC2](https://forum.uipath.com/u/ecSC2)\
**Post date:** [February 2, 2022, 12:18pm UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/7 "2022-02-02T12:18:26Z")

</div>

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** _

---

<div class="post-metadata">

**Author:** ![ydash999](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ydash999/32/289476_2.png) [@ydash999](https://forum.uipath.com/u/ydash999)\
**Post date:** [February 2, 2022, 12:55pm UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/8 "2022-02-02T12:55:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ydash999](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ydash999/32/289476_2.png) [@ydash999](https://forum.uipath.com/u/ydash999)\
**Post date:** [February 2, 2022, 12:57pm UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/9 "2022-02-02T12:57:29Z")

</div>

what is the use of lastor default.trimstart?

---

<div class="post-metadata">

**Author:** ![ecSC2](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ecsc2/32/282242_2.png) [@ecSC2](https://forum.uipath.com/u/ecSC2)\
**Post date:** [February 2, 2022, 1:30pm UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/10 "2022-02-02T13:30:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ydash999](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ydash999/32/289476_2.png) [@ydash999](https://forum.uipath.com/u/ydash999)\
**Post date:** [February 2, 2022, 1:41pm UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/11 "2022-02-02T13:41:54Z")

</div>

Thankss! Very elaborated n helpful!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [February 5, 2022, 1:42pm UTC](https://forum.uipath.com/t/how-to-extract-using-both-substring-and-split-method/396104/12 "2022-02-05T13:42:04Z")

</div>

Closed on February 5, 2022, 1:42pm UTC

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