# Truncate and store part of string

**URL:** <https://forum.robotframework.org/t/truncate-and-store-part-of-string/3950>\
**Category:** Robot Framework\
**Created:** [3 June 2022 15:12 UTC](https://forum.robotframework.org/t/truncate-and-store-part-of-string/3950 "2022-06-03T15:12:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kulkarnivp](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@kulkarnivp](https://forum.robotframework.org/u/kulkarnivp)\
**Post date:** [3 June 2022 15:12 UTC](https://forum.robotframework.org/t/truncate-and-store-part-of-string/3950/1 "2022-06-03T15:12:05Z")

</div>

Hi All,

I have question getting a string for an element using ‘get text’ keyword. Need to truncate and pick only the number out of teh string. Can someone please guide me how to achieve it?

Below is the statement output.  
${get\_medicationCount} = Showing 1 to 25 of 328 entries (filtered from 367 total entries)

I want to truncate and store only value ‘328’ to a variable. Some time it may be 4 digit as well.  
Can someone help me with this.

Thanks,  
Vivek Kulkarni

---

<div class="post-metadata">

**Author:** ![HelioGuilherme66](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/helioguilherme66/32/34_2.png) [@HelioGuilherme66](https://forum.robotframework.org/u/HelioGuilherme66)\
**Post date:** [3 June 2022 15:51 UTC](https://forum.robotframework.org/t/truncate-and-store-part-of-string/3950/2 "2022-06-03T15:51:37Z")

</div>

> [@kulkarnivp](#):
>
> ${get\_medicationCount} = Showing 1 to 25 of 328 entries (filtered from 367 total entries)

Here is a fully functional example (a possible way to do it):

```python
***Settings***
Library String

***Test Cases***
Get Entries
    ${get_medicationCount}= Set Variable Showing 1 to 25 of 328 entries (filtered from 367 total entries)
    @{result}= Get Regexp Matches ${get_medicationCount} Showing (\\d+) to (\\d+) of (\\d+) entries \\(filtered from (\\d+) total entries\\) 1 2 3 4
    Log Many ${result}
    ${entries}= Convert To Integer ${result[0][2]}
    Log The final result is: ${entries}

```

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [4 June 2022 04:12 UTC](https://forum.robotframework.org/t/truncate-and-store-part-of-string/3950/3 "2022-06-04T04:12:06Z")

</div>

Hi Vivek,

Whilst @HelioGuilherme66’s way gives you more control (you can set the regex to only give you the number immediately before " entries"), I understand some people can be intimidated by regex strings.

So another approach is to simply split the string into “words” using either the [Split String](http://robotframework.org/robotframework/latest/libraries/String.html#Split%20String) from String Library, or if you don’t want to import a library, you can so it using the python .split function with the Builtin Evaluate keyword.

```python
    ${get_medicationCount}= Set Variable Showing 1 to 25 of 328 entries (filtered from 367 total entries)
    @{words} =	Split String	${get_medicationCount}
    Log Many ${words}
    ${entries}= Convert To Integer ${words[5]}

```

Or using only BuiltIn Library:

```python
    ${get_medicationCount}= Set Variable Showing 1 to 25 of 328 entries (filtered from 367 total entries)
    @{words} =	Evaluate ${get_medicationCount}.split(" ")
    Log Many ${words}
    ${entries}= Convert To Integer ${words[5]}

```

I think @HelioGuilherme66’s answer is the better solution, just giving you options.

Dave.

---

<div class="post-metadata">

**Author:** ![kulkarnivp](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@kulkarnivp](https://forum.robotframework.org/u/kulkarnivp)\
**Post date:** [4 June 2022 11:02 UTC](https://forum.robotframework.org/t/truncate-and-store-part-of-string/3950/4 "2022-06-04T11:02:33Z")

</div>

Hi Hélio,

Thanks for the solution I’ll try and utilize in the solution.

Thanks,  
Vivek K

---

<div class="post-metadata">

**Author:** ![kulkarnivp](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@kulkarnivp](https://forum.robotframework.org/u/kulkarnivp)\
**Post date:** [4 June 2022 11:36 UTC](https://forum.robotframework.org/t/truncate-and-store-part-of-string/3950/5 "2022-06-04T11:36:48Z")

</div>

Hi Dave,

Thanks for the solution as well. Both I’ve added in my utility will to work.

Thanks,  
Vivek K
