公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.String.slice
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
返回给定字符串的子字符串。如果指定范围超出了字符串的长度,则返回的子字符串会较短。
用法 | 返回 |
---|
String.slice(start, end) | 字符串 |
参数 | 类型 | 详细信息 |
---|
此:string | 字符串 | 要进行子集化的字符串。 |
start | 整数 | 起始索引(含)。负数从字符串末尾开始反向计数。 |
end | 整数,默认值:null | 结束索引(不含)。默认为字符串的长度。负数从字符串末尾开始反向计数。 |
示例
代码编辑器 (JavaScript)
print(ee.String('').slice(0)); // ''
print(ee.String('').slice(-1)); // ''
var s = ee.String('abcdefghijklmnopqrstuvwxyz');
print(s.slice(0)); // abcdefghijklmnopqrstuvwxyz
print(s.slice(24)); // yz
print(s.slice(-3)); // xyz
print(s.slice(3, 3)); // ''
print(s.slice(2, 3)); // c
print(s.slice(-4, 25)); // wxy
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
import ee
import geemap.core as geemap
Colab (Python)
print(ee.String('').slice(0).getInfo()) # ''
print(ee.String('').slice(-1).getInfo()) # ''
s = ee.String('abcdefghijklmnopqrstuvwxyz')
print(s.slice(0).getInfo()) # abcdefghijklmnopqrstuvwxyz
print(s.slice(24).getInfo()) # yz
print(s.slice(-3).getInfo()) # xyz
print(s.slice(3, 3).getInfo()) # ''
print(s.slice(2, 3).getInfo()) # c
print(s.slice(-4, 25).getInfo()) # wxy
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003eString.slice()\u003c/code\u003e extracts a portion of a string, returning a new string.\u003c/p\u003e\n"],["\u003cp\u003eIt uses \u003ccode\u003estart\u003c/code\u003e and \u003ccode\u003eend\u003c/code\u003e indices to define the substring, with \u003ccode\u003estart\u003c/code\u003e being inclusive and \u003ccode\u003eend\u003c/code\u003e exclusive.\u003c/p\u003e\n"],["\u003cp\u003eNegative indices are supported, counting backwards from the string's end.\u003c/p\u003e\n"],["\u003cp\u003eIf the specified range goes beyond the string's boundaries, a shorter substring within those boundaries is returned.\u003c/p\u003e\n"],["\u003cp\u003eIt's applicable in both JavaScript and Python environments within the Earth Engine context.\u003c/p\u003e\n"]]],[],null,["# ee.String.slice\n\nReturns a substring of the given string. If the specified range exceeds the length of the string, returns a shorter substring.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|--------------------------------|---------|\n| String.slice`(start, `*end*`)` | String |\n\n| Argument | Type | Details |\n|----------------|------------------------|---------------------------------------------------------------------------------------------------------------------------------|\n| this: `string` | String | The string to subset. |\n| `start` | Integer | The beginning index, inclusive. Negative numbers count backwards from the end of the string. |\n| `end` | Integer, default: null | The ending index, exclusive. Defaults to the length of the string. Negative numbers count backwards from the end of the string. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\nprint(ee.String('').slice(0)); // ''\nprint(ee.String('').slice(-1)); // ''\n\nvar s = ee.String('abcdefghijklmnopqrstuvwxyz');\nprint(s.slice(0)); // abcdefghijklmnopqrstuvwxyz\nprint(s.slice(24)); // yz\nprint(s.slice(-3)); // xyz\nprint(s.slice(3, 3)); // ''\nprint(s.slice(2, 3)); // c\nprint(s.slice(-4, 25)); // wxy\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\nprint(ee.String('').slice(0).getInfo()) # ''\nprint(ee.String('').slice(-1).getInfo()) # ''\n\ns = ee.String('abcdefghijklmnopqrstuvwxyz')\nprint(s.slice(0).getInfo()) # abcdefghijklmnopqrstuvwxyz\nprint(s.slice(24).getInfo()) # yz\nprint(s.slice(-3).getInfo()) # xyz\nprint(s.slice(3, 3).getInfo()) # ''\nprint(s.slice(2, 3).getInfo()) # c\nprint(s.slice(-4, 25).getInfo()) # wxy\n```"]]