Announcement: All noncommercial projects registered to use Earth Engine before
April 15, 2025 must
verify noncommercial eligibility to maintain Earth Engine access.
ee.String.index
Stay organized with collections
Save and categorize content based on your preferences.
Searches a string for the first occurrence of a substring. Returns the index of the first match, or -1.
Usage | Returns | String.index(pattern) | Integer |
Argument | Type | Details | this: target | String | The string to search. |
pattern | String | The string to find. |
Examples
Code Editor (JavaScript)
print(ee.String('abc123').index('')); // 0
print(ee.String('abc123').index('c1')); // 2
print(ee.String('abc123').index('ZZ')); // -1
// index is case-sensitive.
print(ee.String('abc123').index('BC')); // -1
Python setup
See the
Python Environment page for information on the Python API and using
geemap
for interactive development.
import ee
import geemap.core as geemap
Colab (Python)
print(ee.String('abc123').index('').getInfo()) # 0
print(ee.String('abc123').index('c1').getInfo()) # 2
print(ee.String('abc123').index('ZZ').getInfo()) # -1
# index is case-sensitive.
print(ee.String('abc123').index('BC').getInfo()) # -1
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eindex()\u003c/code\u003e method searches a string for the first occurrence of a specified substring and returns the index of the first match.\u003c/p\u003e\n"],["\u003cp\u003eIf the substring is not found, the method returns -1.\u003c/p\u003e\n"],["\u003cp\u003eThe search performed by \u003ccode\u003eindex()\u003c/code\u003e is case-sensitive.\u003c/p\u003e\n"],["\u003cp\u003eThe method can be used in both JavaScript and Python environments within the Earth Engine platform.\u003c/p\u003e\n"],["\u003cp\u003eAn empty string pattern will return 0 as the index.\u003c/p\u003e\n"]]],["The `index` function searches a string (`target`) for a specified substring (`pattern`). It returns the integer index of the first matching substring's start position. If no match is found, it returns -1. The function is case-sensitive. An empty `pattern` will always return an index of 0. The provided examples illustrate the usage and expected return values.\n"],null,["# ee.String.index\n\nSearches a string for the first occurrence of a substring. Returns the index of the first match, or -1.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|-------------------------|---------|\n| String.index`(pattern)` | Integer |\n\n| Argument | Type | Details |\n|----------------|--------|-----------------------|\n| this: `target` | String | The string to search. |\n| `pattern` | String | The string to find. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\nprint(ee.String('abc123').index('')); // 0\nprint(ee.String('abc123').index('c1')); // 2\nprint(ee.String('abc123').index('ZZ')); // -1\n\n// index is case-sensitive.\nprint(ee.String('abc123').index('BC')); // -1\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('abc123').index('').getInfo()) # 0\nprint(ee.String('abc123').index('c1').getInfo()) # 2\nprint(ee.String('abc123').index('ZZ').getInfo()) # -1\n\n# index is case-sensitive.\nprint(ee.String('abc123').index('BC').getInfo()) # -1\n```"]]