java 查找某字符串

Python013

java 查找某字符串,第1张

import java.io.BufferedReader

import java.io.FileReader

import java.io.IOException

public class Test {

public static void main(String[] args) throws IOException{

//目标字符串

String target = "20000"

//注意修改文件路径

BufferedReader br = new BufferedReader(new FileReader("D:/ks.txt"))

String line = null

while(null != (line = br.readLine())){

if(line.contains(target)){

//截取最后一个分号以后的部分,并作为结果输出

System.out.println("结果为:"+ line.substring(line.lastIndexOf("") + 1))

break

}

}

br.close()

}

}

调用类java.lang.String

的以下方法都可以:

public int indexOf(String str)

返回指定子字符串在此字符串中第一次出现处的索引

参数:

str - 任意字符串。

返回:

如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1。

public int indexOf(String str,int fromIndex)

返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。

参数:

str - 要搜索的子字符串。

fromIndex - 开始搜索的索引位置。

返回:

指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。

public int lastIndexOf(String str)

返回指定子字符串在此字符串中最右边出现处的索引。将最右边的空字符串 "" 视为出现在索引值 this.length() 处。

参数:

str - 要搜索的子字符串。

返回:

如果字符串参数作为一个子字符串在此对象中出现一次或多次,则返回最后一个这种子字符串的第一个字符。如果它不作为一个子字符串出现,则返回 -1。

public int lastIndexOf(String str,int fromIndex)

返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。

参数:

str - 要搜索的子字符串。

fromIndex - 开始搜索的索引位置。

返回:

指定子字符串在此字符串中最后一次出现处的索引。

可以把所有的的输出都看成一个完整的字符串

回车键是 “\n”

String aaa = ""//aaa就是所有的字符串拼起来的字符串

然后用字符串截取 aaa = aaa.subString(0,aaa.length-1)

这样末尾的逗号就去掉了