大概的程序思路是这样的,首先向控制台输出提示信息:请输入一段字符 ,扫描键盘输入并存储到一个变量中(string start)然后再提示,你想在这段字符串中提取的字符,同样扫描键盘输入并存储底到一个变量中。(string over)上面两布 都完成了,现在的重点就是提取字符串了,偶查了一下jsk api,好像要用string类的 substing 方法,而这个api写的晦涩酸苦,对于我这个新手来说,那些示例也看不懂,所以请各位大虾 详细讲解一下。
package src;import java.util.Scanner;public class Pcw_315_3 {
public static void main(String[] args){
Scanner input1=new Scanner(System.in);
System.out.println("请输入一段字符:");
String start = input1.next();
Scanner input2 = new Scanner(System.in);
System.out.println("请输入你想查找的字符串:");
String over=input2.next();
现在写 到这里就卡住了ps:我的拿个over变量用string类型是不是不妥啊
package src;import java.util.Scanner;public class Pcw_315_3 {
public static void main(String[] args){
Scanner input1=new Scanner(System.in);
System.out.println("请输入一段字符:");
String start = input1.next();
Scanner input2 = new Scanner(System.in);
System.out.println("请输入你想查找的字符串:");
String over=input2.next();
现在写 到这里就卡住了ps:我的拿个over变量用string类型是不是不妥啊
System.out.println("请输入一段字符:");
String start = input1.next(); Scanner input2 = new Scanner(System.in);
while (true) {
System.out.println("请输入你想查找的字符串:");
String over = input2.next();
if (over == null || over.length() == 0)
break;
else {
int index = start.indexOf(over);
if (index < 0)
System.out.println("没有找到!");
else
System.out.println(String.format("字串[%s]所在位置为:%d", over, index));
}
}
System.out.println("请输入一段字符:");
String start = input1.next(); Scanner input2 = new Scanner(System.in);
while (true) {
System.out.println("请输入你想查找的字符串:");
String over = input2.next();
if (over == null || over.length() == 0)
break;
else {
int index = start.indexOf(over);
if (index < 0)
System.out.println("没有找到!");
else
System.out.println(String.format("字串[%s]所在位置为:%d", over, index));
}
}搞错了,以为在Delphi呢
用EL写最好!
package csdn;import java.sql.SQLException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class TestDefault {
public static void main(String[] args) throws SQLException {
Scanner input1=new Scanner(System.in);
System.out.println("请输入一段字符:");
String start = input1.nextLine();
if(start == null||start.endsWith("")) {
System.out.println("没有输入数据,请重新来!");
}
//Scanner input2 = new Scanner(System.in);
System.out.println("请输入你想查找的字符串:");
String over=input1.nextLine();
if(over == null||over.endsWith("")) {
System.out.println("没有输入数据,请重新来!");
}
Pattern p = Pattern.compile(over);
Matcher m = p.matcher(start);
int count = 0;
while(m.find()) {
count++;
System.out.println("找到第"+count+"个匹配的位置为:"+m.start()+"到"+m.end());
}
}
}