For each string in the input Tensor, creates a substring starting at index
pos with a total length of len.
If len defines a substring that would extend beyond the length of the input
string, or if len is negative, then as many characters as possible are used.
A negative pos indicates distance within the string backwards from the end.
If pos specifies an index which is out of range for any of the input strings,
then an InvalidArgumentError is thrown.
pos and len must have the same shape, otherwise a ValueError is thrown on
Op creation.
ValueError: If the first argument cannot be converted to a
Tensor of dtype string.
InvalidArgumentError: If indices are out of range.
ValueError: If pos and len are not the same shape.
Args
input
A Tensor of type string. Tensor of strings
pos
A Tensor. Must be one of the following types: int32, int64.
Scalar defining the position of first character in each substring
len
A Tensor. Must have the same type as pos.
Scalar defining the number of characters to include in each substring
unit
An optional string from: "BYTE", "UTF8_CHAR". Defaults to "BYTE".
The unit that is used to create the substring. One of: "BYTE" (for
defining position and length by bytes) or "UTF8_CHAR" (for the UTF-8
encoded Unicode code points). The default is "BYTE". Results are undefined if
unit=UTF8_CHAR and the input strings do not contain structurally valid
UTF-8.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.strings.substr\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/string_ops.py#L441-L445) |\n\nReturn substrings from `Tensor` of strings. \n\n tf.strings.substr(\n input, pos, len, unit='BYTE', name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [tf.data: Build TensorFlow input pipelines](https://www.tensorflow.org/guide/data) - [Ragged tensors](https://www.tensorflow.org/guide/ragged_tensor) - [Unicode strings](https://www.tensorflow.org/text/guide/unicode) |\n\nFor each string in the input `Tensor`, creates a substring starting at index\n`pos` with a total length of `len`.\n\nIf `len` defines a substring that would extend beyond the length of the input\nstring, or if `len` is negative, then as many characters as possible are used.\n\nA negative `pos` indicates distance within the string backwards from the end.\n\nIf `pos` specifies an index which is out of range for any of the input strings,\nthen an `InvalidArgumentError` is thrown.\n\n`pos` and `len` must have the same shape, otherwise a `ValueError` is thrown on\nOp creation.\n| **Note:** `Substr` supports broadcasting up to two dimensions. More about broadcasting [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)\n\n*** ** * ** ***\n\nExamples\n\nUsing scalar `pos` and `len`: \n\n input = [b'Hello', b'World']\n position = 1\n length = 3\n\n output = [b'ell', b'orl']\n\nUsing `pos` and `len` with same shape as `input`: \n\n input = [[b'ten', b'eleven', b'twelve'],\n [b'thirteen', b'fourteen', b'fifteen'],\n [b'sixteen', b'seventeen', b'eighteen']]\n position = [[1, 2, 3],\n [1, 2, 3],\n [1, 2, 3]]\n length = [[2, 3, 4],\n [4, 3, 2],\n [5, 5, 5]]\n\n output = [[b'en', b'eve', b'lve'],\n [b'hirt', b'urt', b'te'],\n [b'ixtee', b'vente', b'hteen']]\n\nBroadcasting `pos` and `len` onto `input`: \n\n input = [[b'ten', b'eleven', b'twelve'],\n [b'thirteen', b'fourteen', b'fifteen'],\n [b'sixteen', b'seventeen', b'eighteen'],\n [b'nineteen', b'twenty', b'twentyone']]\n position = [1, 2, 3]\n length = [1, 2, 3]\n\n output = [[b'e', b'ev', b'lve'],\n [b'h', b'ur', b'tee'],\n [b'i', b've', b'hte'],\n [b'i', b'en', b'nty']]\n\nBroadcasting `input` onto `pos` and `len`: \n\n input = b'thirteen'\n position = [1, 5, 7]\n length = [3, 2, 1]\n\n output = [b'hir', b'ee', b'n']\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|---|---|\n| \u003cbr /\u003e - `ValueError`: If the first argument cannot be converted to a Tensor of `dtype string`. - `InvalidArgumentError`: If indices are out of range. - `ValueError`: If `pos` and `len` are not the same shape. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `input` | A `Tensor` of type `string`. Tensor of strings |\n| `pos` | A `Tensor`. Must be one of the following types: `int32`, `int64`. Scalar defining the position of first character in each substring |\n| `len` | A `Tensor`. Must have the same type as `pos`. Scalar defining the number of characters to include in each substring |\n| `unit` | An optional `string` from: `\"BYTE\", \"UTF8_CHAR\"`. Defaults to `\"BYTE\"`. The unit that is used to create the substring. One of: `\"BYTE\"` (for defining position and length by bytes) or `\"UTF8_CHAR\"` (for the UTF-8 encoded Unicode code points). The default is `\"BYTE\"`. Results are undefined if `unit=UTF8_CHAR` and the `input` strings do not contain structurally valid UTF-8. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` of type `string`. ||\n\n\u003cbr /\u003e"]]