indexofdemo.java

来自「JAVA编程思想源代码 值得一下 很难找的」· Java 代码 · 共 41 行

JAVA
41
字号
package chapter8;

public class IndexOfDemo {

	public static void main(String[] args) {
		String s1 = "AbCdEAbCdE";
		String s2 = "Ab";
		System.out.println("s1:" + s1 + "     s2:" + s2);
		System.out.println("从头向后查找。。。。。。");
		if (s1.indexOf(s2) != -1) {
			System.out.println("s2是s1的子串");
			System.out.println("s2在s1中的位置为:" + s1.indexOf(s2));
		} else {
			System.out.println("s2不是s1的子串");
		}
		System.out.println("从第二个字符开始向后查找。。。。。。");
		if (s1.indexOf(s2, 1) != -1) {
			System.out.println("s2是s1的子串");
			System.out.println("s2在s1中的位置为:" + s1.indexOf(s2, 1));
		} else {
			System.out.println("s2不是s1的子串");
		}
		System.out.println("从字符串末尾开始向前查找。。。。。。");
		if (s1.lastIndexOf(s2) != -1) {
			System.out.println("s2是s1的子串");
			System.out.println("s2在s1中的位置为:" + s1.lastIndexOf(s2));
		} else {
			System.out.println("s2不是s1的子串");
		}
		System.out.println("从字符串末尾第三个字符开始向前查找。。。。。。");
		if (s1.lastIndexOf(s2, 2) != -1) {
			System.out.println("s2是s1的子串");
			System.out.println("s2在s1中的位置为:" + s1.lastIndexOf(s2, 2));
		} else {
			System.out.println("s2不是s1的子串");
		}

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?