[英]How to know if there is some code before a comment in java using kotlin?
我有興趣編寫一些代碼來檢測 java 代碼中的特殊情況,我們在同一行上有一些注釋的代碼。
int i = 10 //here is the comment
我希望能夠使用 kotlin 的 File forEachLine 方法檢測此類行。 但是我不知道該怎么做。
我唯一能做的就是通過以下方式找到包含評論的行:
File(fileName).forEachLine {
if(it.contains("//")){
println("There is a comment!")
}
}
我對檢測注釋不感興趣,只對那些在同一行有代碼和注釋的行感興趣。
注意:fileName 是一個 java 文件,例如:我們逐行讀取的 Test.java。
您可以在文件的每一行上組合使用split
和filter
函數:
fun getCodeLinesWithComments(lines: ArrayList<String>): ArrayList<String> {
val codeLinesWithComments = arrayListOf<String>()
for (line in lines) {
if (line.split("//").filter { it.isNotEmpty() }.count() == 2) {
codeLinesWithComments.add(line)
}
}
return codeLinesWithComments
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.