LoginSignup
1
0

More than 5 years have passed since last update.

C#で比較対象の文字列が同じか判定する

Last updated at Posted at 2018-07-20

C#では、stringクラスを使って文字列を扱います。
Stringは参照型ですが、等値演算子(C#では==)を使って、文字列の内容自体が同じかどうかが比較されます。
等値演算子で文字列を比較すると、2つの文字列が全く同じかを確認できます。つまり、大文字小文字を区別し、カルチャにも依存しない比較(序数比較、バイナリ比較)を行います。

string s1 = "test";
string s2 = "Test";
if (s1 == s2) {
   Debug.WriteLine("等しい");
} else {
   Debug.WriteLine("等しくない");
}
結果:
等しくない

注意:
C#では、Object型にキャストされている文字列を比較するときに==演算子を使うと、参照の等価を調べることになってしまうこと

1
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0