challenge 条件を満たす行を取り除く

ファイルから1行ずつ読み込み、"#"で始まる行だけを取り除いてファイルに出力するコードを書いてください。

サンプル入力

hello!
# remove this
 # don't remove this
bye!
サンプル出力
hello!
 # don't remove this
bye!

Posted feedbacks - VB.net

1行ずつ読み込みということなのでReadLineを使用してます。ただ、まとめて読み込んでも短くはならないと思うけど。
Usingステートメントも使用できるけど、例外処理いれることを考えたらTryだけ使うほうがいいよね?
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Public Sub FilterFile(ByVal input As String, ByVal output As String)

    Dim sr As StreamReader = Nothing
    Dim sw As StreamWriter = Nothing

    Try
        sr = New StreamReader(input)
        sw = New StreamWriter(output)

        Do While sr.Peek <> -1
            Dim line As String = sr.ReadLine
            If Not line.StartsWith("#") Then
                sw.WriteLine(line)
            End If
        Loop

    Catch ex As Exception

    Finally
        If Not sr Is Nothing Then
            sr.Close()
        End If
        If Not sw Is Nothing Then
            sw.Close()
        End If
    End Try

End Sub

Index

Feed

Other

Link

Pathtraq

loading...