课程表

Groovy课程

工具箱
速查手册

Groovy indexOf()方法

当前位置:免费教程 » Java相关 » Groovy

返回此字符串中指定子字符串第一次出现的索引。此方法有4种不同的变体。

  • public int indexOf(int ch) - 返回此字符串中指定字符首次出现时的索引,如果字符未出现则返回-1。

句法

  1. public int indexOf(int ch)

参数

ch - 在字符串中搜索的字符。

返回值

返回此字符串中指定字符首次出现的索引,如果字符不出现,则返回-1。

  • public int indexOf(int ch,int fromIndex) - 返回指定字符第一次出现的字符串中的索引,在指定索引处开始搜索,如果字符未出现则返回1。

返回值

  1. public int indexOf(int ch, int fromIndex)

参数

  • ch - 要在字符串中搜索的字符

  • fromIndex - 从哪里开始搜索

返回值

返回此字符串中指定字符第一次出现的索引,在指定索引处开始搜索,如果字符未出现,则返回-1。

  • int indexOf(String str) - 返回此字符串中指定子字符串第一次出现的索引。 如果不作为子字符串出现,则返回-1。

参数

  1. int indexOf(String str)

参数

Str - 要搜索的字符串

返回值

返回此字符串中指定子字符串第一次出现的索引。 如果不作为子字符串出现,则返回-1。

  • int indexOf(String str,int fromIndex) - 返回指定子字符串第一次出现的字符串中的索引,从指定的索引开始。 如果不发生,则返回-1。

参数

  1. int indexOf(String str, int fromIndex)

参数

str - 要搜索的字符串

  • fromIndex – where to start the search from

返回值 - 返回指定子字符串第一次出现的此字符串中的索引,从指定的索引开始。 如果不发生,则返回-1。

以下是所有4种方法变体的使用示例

  1. class Example {
  2. static void main(String[] args) {
  3. String a = "Hello World";
  4. // Using public int indexOf(int ch)
  5. println(a.indexOf('e'));
  6. println(a.indexOf('o'));
  7. // Using public int indexOf(int ch, int fromIndex)
  8. println(a.indexOf('l',1));
  9. println(a.indexOf('e',4));
  10. // Using public int indexOf(string str)
  11. println(a.indexOf('el'));
  12. println(a.indexOf('or'));
  13. // Using public int indexOf(string str,int fromIndex)
  14. println(a.indexOf('el',1));
  15. println(a.indexOf('or',8));
  16. }
  17. }

当我们运行上面的程序,我们将得到以下结果 -

  1. 1
  2. 4
  3. 2
  4. -1
  5. 1
  6. 7
  7. 1
  8. -1
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号