ee.String.index
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tìm kiếm chuỗi để tìm lần xuất hiện đầu tiên của một chuỗi con. Trả về chỉ mục của kết quả trùng khớp đầu tiên hoặc -1.
Cách sử dụng | Giá trị trả về |
---|
String.index(pattern) | Số nguyên |
Đối số | Loại | Thông tin chi tiết |
---|
this: target | Chuỗi | Chuỗi cần tìm. |
pattern | Chuỗi | Chuỗi cần tìm. |
Ví dụ
Trình soạn thảo mã (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
Thiết lập Python
Hãy xem trang
Môi trường Python để biết thông tin về API Python và cách sử dụng geemap
cho quá trình phát triển tương tác.
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
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 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```"]]