前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站​点击跳转浏览。​

substring()方法

String str = "hello world!";
System.out.println(str.substring(1));
System.out.println(str.substring(3));
System.out.println(str.substring(6));
执行以上代码,结果如下:

ello world!
lo world!
world!

indexOf()方法

indexOf()方法用于在String类的对象中查找子字符串,方法返回一个整数值,为子字符串的开始位置

String str = "abcdefghijklmnabc";
System.out.println(str1.indexOf("c")); //结果为2,为字符"c"第一次出现的位置
System.out.println(str1.indexOf("x")); //结果为-1,没有找到字符"x"

lastIndexOf()方法

Java查找字符串最后一次出现的位置

package com.qf.chapter02;

public class TestStringSearch {
public static void main(String[] args) {
String string="hello world ,java is the best in the world";
int lastIndex=string.lastIndexOf("world");//字符串第一个字符最后出现的下标
if(lastIndex==-1) {
System.out.println("不存在字符串 world");
}else {
System.out.println("字符串world最后一次出现的位置:"+lastIndex);
}
}
}

字符串world最后一次出现的位置:37