Skip to content

Documentation for "substring" and Similar API #60230

Open
@RohitSaily

Description

@RohitSaily

The String API documentation for substring states

The substring of this string from start, inclusive, to end, exclusive.

It is reasonable to interpret this as start is a valid index because it is inclusive and no relation to end or length is specified at this point. Then the end of the documentation states

Both start and end must be non-negative and no greater than length;

Which implies start==length is valid. This can be confusing due to the earlier statement. Furthermore, it is not intuitive that start==end==length is a valid case but a similar/symmetrical case start==-1 and end==0 is not valid. Both are empty ranges adjacent to the range of valid indices. In my personal experience, I did not understand this nuance until I was testing code.


I think the documentation could benefit from clarifying this. Perhaps starting with the statement 0 ≤ start ≤ end ≤ length must hold. would clearly and quickly communicate the valid possible inputs. This change could also be applied to similar API e.g. List.sublist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-coretype-documentationA request to add or improve documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions