私は1行ずつ検索して特定の文字列を探し、それに応じていくつかのアクションを取らなければならないhtmlファイルを持っています。 問題は、文字列がhtmlファイルの各行の行全体と一致していることです。 したがって、与えられた行の実際の文字列の前にスペースがある場合、一致が肯定であるにもかかわらず偽となります。javaを使用してhtmlファイルの文字列検索で空白を無視する方法はありますか?
package read_txt;
import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.html");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
//String a = "media query";
switch (strLine) {
case "@media query" :
System.out.println("media query found");
System.out.println("html file responsive");
break;
// default :
// System.out.println("html file unresponsive");
//break;
}
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
私のコードでは、私は文字列 "メディアクエリ"を探しています。
コードこのHTMLファイルを正常に動作しますが、今私たちは、このhtmlファイルがあるとします:
文字列の一致はありませんが、今、これが検索されているhtmlファイルがされているとメディアクエリ文字列が存在しても動作しませんが、一致した文字列を "メディアクエリ"ではなく "メディアクエリ"に変更すると、再び動作します。 どのような行のテキストが表示される前に発生する空白のスペースを無視することができますか?
'' String''の '' trim() ''メソッドを見てください... – f1sh
ありがとうございます。それを見てください –