
正文
java代码中搜索关键字,java实现关键字搜索
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java中怎么实现在一个字符串中查找其中的关键字。
public class $ {
public static void main(String... _) {
String str = "123456789 abcdefg hijklmn...";
System.out.println(str.indexOf("456"));
System.out.println(str.indexOf("45a"));
}
}
结果:
3
-1
如果有,就返回他的起始位置,注意是从0开始
没有,就返回-1
用循环
String[] key = { "456", "abc", "45a" };
String str = "123456789 abcdefg hijklmn...";
for (int i = 0; i key.length; i++) {
System.out.println(key[i] + "的起始位置:" + str.indexOf(key[i]));
}
相关问答
Q1: 用java语言编写一个识别关键字的程序
package testWrite;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class write {
InputStream inStream ;
FileOutputStream fs ;
int bytesum = 0;
int byteread = 0;
String data ;
StringBuffer dd;
public static byte[] readdata(String filePath) {
byte[] data = new byte[2048];// 用于存储读取的文件内容
try {
File file = new File(filePath);
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
fis.read(data);
fis.close();
}else{
System.out.println("文件不存在");
}
} catch ( Exception e) {
}
return data;
}
public void writeFile(String fileCopyPath,byte [] data){
File file = new File(fileCopyPath);
try {
if (!file.exists()) {
file.createNewFile(); //创建文件
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
}else{
System.out.println("文件已经存在");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package testWrite;
public class writeOne extends write {
public static void main(String[] args) {
//第一种
// writeOne a=new writeOne ();
// byte [] data=a.readdata("D:\\Users\\workspace\\testWrite\\src\\testWrite\\write.java");
// a.writeFile("D:\\Users\\workspace\\testWrite\\src\\testWrite\\writenew.java",data);
//第二种
writeOne a2=new writeOne ();
System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
String path=System.getProperty("user.dir");
System.out.println(path);
String writefile="\\src\\testWrite\\write.java";
String writenewfile="\\src\\testWrite\\writenew.java";
String w1=path+writefile; //读取文件的路径
String w2=path+writenewfile; //写入文件路径
byte [] data=a2.readdata(w1); //读取文件
a2.writeFile(w2 ,data); //写入文件
}
}
本地已经调试通过可以复制write.java 文件 ,注意包,和文件的路径 直接运行第二个java文件就可以了,运行后刷新一下就可以看到复制的文件(备注:我是使用myeclipse环境进行调试)
Q2: java中怎么实现在一个字符串中查找其中的关键字
你好:
可以看看API,
类 String
indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引。
关键字存在字符串中则返回偏移(即索引),从0位开始;
Q3: Java怎么指定文件里搜索关键字
得看你的文件的格式了。
如果你文件的格式是每行一个
姓名,电话,个人信息
这样的,那么就会特别简单。
步骤:
读取文件,按行读取
readline
检查这一行是否有
你需要的电话号码,有解析这行的姓名和个人信息。
Q4: java 编程,搜索txt中关键字
import java.io.*;
public class Keyword{
public void test(String keyword)throws IOException{
String fileName = "MagazineList.txt";
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String nextLine;
int line=1;
while((nextLine=br.readLine())!=null){
if(nextLine.indexOf(keyword)-1){
System.out.println(line+"行 "+nextLine);
}
line++;
}
}
public static void main(String[] args)throws IOException{
new Keyword().test("java");
}
}
一个完整的例子,请参考
Q5: 找出java源代码中的java关键字
把Java所有的关键字存到map等容器里边(总共也没几个),判断单词是否是关键字,再用正则表达式判断是字符串,注释还是关键字,若是则加颜色,否则不加颜色。
java代码中搜索关键字的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现关键字搜索、java代码中搜索关键字的信息别忘了在本站进行查找喔。







