[java practices]查找字符串出现的位置

  1. package com.company;  
  2.   
  3. import java.io.*;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import java.util.Scanner;  
  7.   
  8. public class Main {  
  9.     public static void main(String[] args) throws IOException  
  10.     {  
  11.   
  12.         File f = new File("c:/code.txt");  
  13.         FileReader fr = new FileReader(f);  
  14.         BufferedReader br = new BufferedReader(fr);  
  15.         String strRead = null;  
  16.         List<String> lstLines = new ArrayList<String>();  
  17.         int nLineNum = 1;  
  18.         while((strRead=br.readLine()) != null)  
  19.         {  
  20.             lstLines.add(strRead);  
  21.         }  
  22.         System.out.println("请输入要查找的单词");  
  23.         Scanner sc = new Scanner(System.in);  
  24.         String strWord = sc.nextLine();  
  25.         for(String strTemp : lstLines)  
  26.         {  
  27.             boolean b = ContainsStr(strTemp,strWord);  
  28.             if(b)  
  29.             {  
  30.                 System.out.println(nLineNum + ":" + strTemp);  
  31.             }  
  32.             nLineNum++;  
  33.         }  
  34.     }  
  35.     public static boolean ContainsStr(String s1, String s2)  
  36.     {  
  37.         if(s1.indexOf(s2) >=0 )  
  38.             return true;  
  39.         else  
  40.             return false;  
  41.     }  
  42. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值