C#CompareOrdinal()メソッド
C#のCompareOrdinal()メソッドは、各文字列の対応するCharオブジェクトの数値を評価することにより、指定された2つのStringオブジェクトを比較するために使用されます。
構文
public static int CompareOrdinal (string str1, string str2);
上記のstr1とstr2は、比較する文字列です。
戻り値はゼロ未満、str1
例
using System;
public class Demo {
public static void Main(string[] args) {
string s1 = "Amy";
string s2 = "Katie";
string s3 = s2;
Console.WriteLine("String1 = "+s1);
Console.WriteLine("String2 = "+s2);
Console.WriteLine("Are both the strings equal? = "+s1.Equals(s2));
Console.WriteLine("Are both the strings equal? = "+s2.Equals(s3));
Console.WriteLine(string.CompareOrdinal(s1, s2));
Console.WriteLine(string.CompareOrdinal(s2, s3));
}
} 出力
String1 = Amy String2 = Katie Are both the strings equal? = False Are both the strings equal? = True -10 0
例
using System;
public class Demo {
public static void Main(string[] args) {
string s1 = "David";
string s2 = "David";
string s3 = s2;
string s4 = "Tom";
string s5 = s4;
Console.WriteLine("String1 = "+s1);
Console.WriteLine("String2 = "+s2);
Console.WriteLine("String3 = "+s3);
Console.WriteLine("String4 = "+s4);
Console.WriteLine("String5 = "+s5);
Console.WriteLine("Is s1 and s2 equal? = "+s1.Equals(s2));
Console.WriteLine("Is s2 and s3 equal? = "+s2.Equals(s3));
Console.WriteLine("Is s3 and s4 equal? = "+s3.Equals(s4));
Console.WriteLine("Is s4 and s5 equal? = "+s4.Equals(s5));
Console.WriteLine(string.CompareOrdinal(s1, s2));
Console.WriteLine(string.CompareOrdinal(s2, s3));
Console.WriteLine(string.CompareOrdinal(s3, s4));
Console.WriteLine(string.CompareOrdinal(s4, s5));
}
} 出力
String1 = David String2 = David String3 = David String4 = Tom String5 = Tom Is s1 and s2 equal? = True Is s2 and s3 equal? = True Is s3 and s4 equal? = False Is s4 and s5 equal? = True 0 0 -16 0
-
C#のConsole.ResetColor()メソッド
C#のConsole.ResetColor()メソッドは、コンソールの前景色と背景色をデフォルトに設定するために使用されます。 構文 構文は次のとおりです- public static void ResetColor (); 例 ここで、C#でConsole.ResetColor()メソッドを実装する例を見てみましょう- using System; class Demo { public static void Main (string[] args) { Console.WriteLine ("At first
-
C#のConsole.MoveBufferArea()メソッド
C#のConsole.MoveBufferArea()メソッドは、画面バッファーの指定されたソース領域を指定された宛先領域にコピーするために使用されます。 構文 構文は次のとおりです- public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop); ここで sourceLeft ソース領域の左端の列。 sourceTop ソース領域の一番上の行。 sourceWidt