2015-05-17 253 views
2

我正在閱讀文本文件,條件是以*開始的單詞將被忽略。從條件中讀取文本文件

example: 
abc 1234 *text to be ignored 

因此,在這個例子中,我會忽略「的文字被忽略」從文本文件閱讀,只會店ABC和1234字符串數組時。

爲此,我寫了下面的代碼。我如何才能達到忽略以*開頭的單詞的條件?

public static void read(String filename) { 
     BufferedReader reader = null; 

     try { 
      String line; 
      reader = new BufferedReader (new FileReader(filename)); 
      while ((line = reader.readLine()) != null) { 
       String[] functionName = line.split("\\s+");   
          } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } finally { 
      if (reader != null) 
       try { 
        reader.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
     } 
    } 
+0

所以,如果你看到一個''你要不顧一切從這個嗎?剩下的尾部空間呢?另外,你使用的是什麼Java版本? – fge

回答

2

startWith(String literal)如果您的字符串以給定的字符串字面值開頭,則返回true。

例如:

"1234".startsWith("12");返回true

所以你應該閱讀所有的單詞,並檢查它是否開始,甚至包含*,如果是的話,然後忽略整個單詞。

例子:

if(! word.startsWith("*")) { 
// add to what ever you want 
} 

if(! word.contains("*")) { 
// add to what ever you want 
} 
+1

不,.startsWith()不會將正則表達式作爲參數,而是一個字符串文字。 – fge

+0

編輯答案,謝謝你讓我知道,不知道。 –

1

你可以嘗試用indexOf()substring()

while ((line = reader.readLine()) != null) { 
    if(line.indexOf("*")>-1) 
    line=line.substring(0,line.indexOf("*")); 
    String[] functionName = line.split("\\s+"); 
} 

上面有哪些indexOf("*")會給你01指數那麼你可以通過做substring(beginIndex,endIndex)

+0

我認爲,提供的所有最好的答案都是 – Razib

+0

如果有_no_星怎麼辦? – fge

+0

@fge OP dint提到,但如果只是放一個'if(line.indexOf(「*」)> - 1)'我已經更新了我的答案 – silentprogrammer

1

你可以做這樣的事情在你的while循環只要找到與endIndex*indexOf("*")發現該指數的子 -

while ((line = reader.readLine()) != null) { 
    String[] functionName = line.split("\\s+");   
    String newLine = ""; 

    for(String strg : functionName){ 

     if(strg.startsWith("*")){ 
     break; 
     }else{ 
     newLine = strg + newLine; 
     } 

    } 
} 
1

你不告訴什麼你正在使用的Java版本,所以我打算假設Java 8 ...

注:代碼未經測試,但它應該適用於一些改編。

private static final Pattern SPACES = Pattern.compile("\\s+"); 
private static final Pattern STAR_TO_END = Pattern.compile("\\s*\\*.*"); 
public static String[] read(final String filename) 
{ 
    final Path path = Paths.get(filename); 

    try (
     // UTF-8 by default; excellent 
     final Stream<String> lines = Files.line(path); 
    ) { 
     return lines.map(line -> STAR_TO_END.matcher(line).replaceFirst("")) 
      .flatMap(SPACES::splitAsStream) 
      .collect(Collectors.toArray(String[]::new)); 
    } 
} 
1

如果你不想通過你的話循環來檢查它是否有一個*開始你也可以從他們之前使用split刪除所有帶*號的單詞。

String str = "abc 1234 *text to be ignored"; 
System.out.println(Arrays.toString(str.replaceAll("\\*[^\\s]+\\s*", "").split("\\s+"))); 
// [abc, 1234, to, be, ignored] 
str = "*abc *1234 *text to be *ignored"; 
System.out.println(Arrays.toString(str.replaceAll("\\*[^\\s]+\\s*", "").split("\\s+"))); 
// [to, be] 

正則表達式擊穿

\\* - Literal match of asterisk 
[^\\s]+ - Match anything but a space 
\\s* - Capture any or no spaces at end of word