获取子字符串索引位置--indexOf方法与lastIndexOf方法

た 入场券 2024-02-18 18:16 86阅读 0赞
  1. package com;
  2. public class GetStringIndex {
  3. public static void main(String[] args) {
  4. String str ="明月几时有,把酒问青天";
  5. System.out.println(str);
  6. int charIndex =str.indexOf("酒");
  7. System.out.println("字符‘酒’首次出现的索引为:"+charIndex);
  8. }
  9. }

20180221114139222

-——————————————————————————————————————————-

  1. package com;
  2. public class GetStringIndex {
  3. public static void main(String[] args) {
  4. String str ="we are the world";
  5. System.out.println(str);
  6. int charIndex =str.indexOf("e");
  7. System.out.println("字符‘e’首次出现的索引为:"+charIndex);
  8. int charIndex2 =str.indexOf("e",4);
  9. System.out.println("字符‘e’在索引4之后第一次出现的位置:"+charIndex2);
  10. System.out.println();
  11. String str2 = "let it go!let it go!";
  12. System.out.println(str2);
  13. int index = str2.lastIndexOf("t");
  14. System.out.println("字符t最后一次出现的位置:"+index);
  15. int index2 =str2.lastIndexOf("t",14);
  16. System.out.println("从索引14的位置往前查第一个t出现的位置:"+index2);
  17. }
  18. }

20180224144455452

发表评论

表情:
评论列表 (有 0 条评论,86人围观)

还没有评论,来说两句吧...

相关阅读