⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 findsubstring.java

📁 一些Java的小的应用程序
💻 JAVA
字号:
//==============Find subString====================
/** 利用自编方法寻找子字符串 */
public class FindsubString {
	public static void main(String args[]) {
		final int Len = 100;
		byte buf1[] = new byte[Len];
		System.out.println("Enter a string");
		try {
			System.in.read(buf1, 0, Len);
		} catch (Exception e) {
		}
		String s1 = new String(buf1);
		s1 = s1.trim();
		int n1 = s1.length();
		byte buf2[] = new byte[Len];
		System.out
				.println("Enter a substring of the string that you have entered.");
		try {
			System.in.read(buf2, 0, Len);
		} catch (Exception e) {
		}
		String s2 = new String(buf2);
		s2 = s2.trim();
		int n2 = s2.length();
		System.out.println("n1= " + n1 + "\tn2 =" + n2);
		boolean found = false;
		int k = 0;
		stop: for (int i = 0; n2 + i <= n1; i++)
			for (int j = 0; j < n2; j++) {
				System.out.println(i + "  " + j);
				if (s1.charAt(i + j) != s2.charAt(j))
					break;
				if (j + 1 == n2) {
					found = true;
					k = i;
					break stop;
				}
			}
		System.out.print("Using this algorithm,the substring \"" + s2);
		if (found)
			System.out.println("\" was found at index " + k);
		else
			System.out.println("\" was not found!");
		k = s1.indexOf(s2);
		System.out.print("Using the indexOf method,the substring \"" + s2);
		if (found)
			System.out.println("\" was found at index " + k);
		else
			System.out.println("\" was not found!");
	}
}

⌨️ 快捷键说明

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