Truncate and store part of string

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

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

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

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 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.

    ${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:

    ${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.

2 Likes

Hi Hélio,

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

Thanks,
Vivek K

1 Like

Hi Dave,

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

Thanks,
Vivek K

1 Like