From 8f106a2b9a7824075e55d3f044f2c0c5dd3ee700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Cerveau?= Date: Thu, 9 Jul 2020 12:34:34 +0200 Subject: string: add substring method This method aims to offer a simple way to 'substring' an existing string with start and end values. --- docs/markdown/Reference-manual.md | 3 +++ docs/markdown/Syntax.md | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) (limited to 'docs/markdown') diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md index 966d408..080fe3e 100644 --- a/docs/markdown/Reference-manual.md +++ b/docs/markdown/Reference-manual.md @@ -1977,6 +1977,9 @@ are immutable, all operations return their results as a new string. - `startswith(string)`: returns true if string starts with the string specified as the argument +- `substring(start,end)` *(since 0.56.0)*: returns a substring specified from start to end. + Both `start` and `end` arguments are optional, so, for example, `'foobar'.substring()` will return `'foobar'`. + - `strip()`: removes whitespace at the beginning and end of the string. *(since 0.43.0)* Optionally can take one positional string argument, and all characters in that string will be stripped. diff --git a/docs/markdown/Syntax.md b/docs/markdown/Syntax.md index 7cb39e9..bbe3dbb 100644 --- a/docs/markdown/Syntax.md +++ b/docs/markdown/Syntax.md @@ -220,6 +220,26 @@ is_x86 = target.startswith('x86') # boolean value 'true' is_bsd = target.to_lower().endswith('bsd') # boolean value 'true' ``` +#### .substring() + +Since 0.56.0, you can extract a substring from a string. + +```meson +# Similar to the Python str[start:end] syntax +target = 'x86_FreeBSD' +platform = target.substring(0, 3) # prefix string value 'x86' +system = target.substring(4) # suffix string value 'FreeBSD' +``` + +The method accepts negative values where negative `start` is relative to the end of +string `len(string) - start` as well as negative `end`. + +```meson +string = 'foobar' +target.substring(-5, -3) # => 'oo' +target.substring(1, -1) # => 'ooba' +``` + #### .split(), .join() ```meson -- cgit v1.1