Skip to content

intelligent slice functionality for strings #413

Closed
@aman-godara

Description

@aman-godara

Description

Name of functionality: Slice
Signature: slice(string, start(optional)= 1 or len(string), end(optional)=len(string) or 1, stride(optional)= 1, include_start(optional)= .true.)
Output: a new string_type object

Traverses the input string from start index to end index taking a stride of stride indexes to return a new string. start can be greater than end as well giving function an added functionality of reversing input string. start index will always be included in the output substring unless include_start is set to .false. where the end index will be included. So either start index or end index will be included in the output string (or both).
This function is an intelligent one, if the user doesn’t provide any one or more of the 3 optional arguments (start, end or stride) it figures them out automatically using optional arguments which are given (see examples).
But if the user provides arguments he/she is expected to be responsible for that (see example 3).

Examples:

  1. slice(‘12345’, stride=-1) should give '54321'; start = 5, end = 1
  2. slice(‘abcd’, stride=-2, include_start=.false.) should give 'ca'; start = 4, end = 1
  3. slice(‘abcde’, 5, 2, 1) will give '' (empty string); user gave 1 as the stride argument
  4. slice(‘abcde’, 5, 2) will give 'edcb'; stride = -1
  5. slice(‘abcde’, end = 1, stride = -2) will give 'eca'; start = 5

Prior Art

Python has it, but not exactly in the manner proposed above. In python one can do

str = 'spiderman'
print(str[1:9:2])

which will return 'pdra'.

Please review the functionality and let me know your thoughts on this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ideaProposition of an idea and opening an issue to discuss it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions