简体   繁体   English

忽略Java中以“#”开头的文件中的行

[英]Ignore lines in file starting with “#” sign in Java

I'm trying to parse a text file in Java. 我正在尝试解析Java中的文本文件。 In this text file, comments(things I don't want to parse) are denoted by a "#" sign at the beginning of the line. 在此文本文件中,注释(我不想解析的内容)在该行的开头用“#”符号表示。

I thought this would be really simple, something like: 我认为这将非常简单,例如:

String line ="#hi everyone";

if(!line.startsWith("#")){
//do stuff
}

But it doesn't work. 但这是行不通的。 I've also tried using !line.substring(0,1).equals("#"), .contains, and messed around with chars and more complicated regex stuff but with no luck. 我还尝试过使用!line.substring(0,1).equals(“#”)、. contains,并弄乱了chars和更复杂的regex内容,但是没有运气。

I've watched it in the debugger and checked to make sure that the value for "line" is actually the correct value. 我已经在调试器中查看了它,并检查以确保“ line”的值实际上是正确的值。 It is correct, the line starts with #, and yet it somehow is passing the test in my if statement. 没错,该行以#开头,但是以某种方式通过了我的if语句中的测试。

Am I missing something really basic here? 我在这里真的缺少基本的东西吗?

Have you tried 你有没有尝试过

String line ="#hi everyone";

if(!line.charAt(0) == '#'){
//do stuff
}

try either of the following: 请尝试以下任一方法:

regex: "^[ \\t]*#.*"

or 要么

line.trim().startsWith("#")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM