文字列(String)が空の文字列かを調べるに方法にて説明します。
等値演算子による比較
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Dim s As String = "" If s Is Nothing Then MessageBox.Show("文字列はNULLです") End If If s = "" Then MessageBox.Show("文字列は空です") End If If s = String.Empty Then MessageBox.Show("文字列はEmptyです") End If |
Equals メソッドによる比較
1 2 3 4 5 6 7 8 9 |
Dim s As String = "" If s.Equals("") Then MessageBox.Show("文字列は空です") End If If s.Equals(String.Empty) Then MessageBox.Show("文字列はEmptyです") End If |
Nothing (NULL) か比較
1 2 3 4 5 |
Dim s As String = "" If s Is Nothing Then MessageBox.Show("文字列はNULLです") End If |
※文字列は
Dim s As String
Dim s As String = “”
Dim s As String = Nothing
※以上3つとも値がことなりますので注意