줄 단위로 검색하고 특정 문자열을 찾은 다음 그에 따라 몇 가지 작업을 수행해야하는 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 파일이 있다고 가정 :
문자열 일치하지를 미디어 쿼리 문자열이 있지만 작동하지 않지만 "미디어 쿼리"대신 "미디어 쿼리"와 일치하는 문자열을 변경하면 다시 작동합니다. 줄에 텍스트가 나타나기 전에 간격이있는 공백을 어떻게 무시할 수 있습니까?
"문자열"의'trim()''메소드를보십시오 ... – f1sh
제안 해 주셔서 감사합니다. 살펴보기 –