首页 > 解决方案 > 将字符串与文件中的另一个字符串进行比较

问题描述

我已经尝试了“.equals”、“.contains”和“==”我仍然没有得到结果

 String pre = "0917";
        File file = new File("F:\\Eclipse\\Loading De Luna\\bin\\GlobePrefix.txt"); 

          BufferedReader br = new BufferedReader(new FileReader(file)); 

          String prefix; 
          while ((prefix = br.readLine()) != null)
              if(prefix.equals(pre))
                  System.out.println("Found");
              else  System.out.println("Not Found");

.txt 文件包含以下字符串:

0817 
0905 
0906 
0915 
0916 
0917 
0926 
0927 
0935 
0936 
0937 
0945 
0955 
0956 
0965

标签: javastringcompare

解决方案


if (prefix.trim().equals(pre)) System.out.println("Found"); else System.out.println("Not Found");

会解决问题。


推荐阅读