指定した 2 つの String オブジェクトを比較します。比較時に大文字小文字を区別するかどうかを設定できます。
Overloads Public Shared Function Compare( _
ByVal strA As String, _ ByVal strB As String, _ ByVal ignoreCase As Boolean _) As Integer
[C#]
public static int Compare(stringstrA,stringstrB,boolignoreCase);
[C++]
public: static int Compare(String* strA,String* strB,boolignoreCase);
[JScript]
public static function Compare(
strA : String,strB : String,ignoreCase : Boolean) : int;
パラメータ
- strA
第 1 の String 。 - strB
第 2 の String 。 - ignoreCase
大文字と小文字を区別して比較するか、区別せずに比較するかを示す Boolean 。(true は、大文字と小文字を区別せずに比較することを示します。
戻り値
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。
| 値 | 意味 |
|---|---|
| 0 より小 | strA が strB より小さい。 |
| 0 | strA と strB は等しい。 |
| 0 より大 | strA が strB より大きい。 |
解説
比較では、現在のカルチャを使用して、大文字と小文字の規則や個々の文字のアルファベット順など、カルチャ固有の情報を取得します。たとえば、カルチャでは、1 つの文字として扱う特定の文字の組み合わせ、大文字と小文字を特定の方法で比較するかどうか、前後の文字に基づいた文字の並べ替えの基準などを規定します。
比較は、単語の並べ替え規則を使用して実行されます。単語、文字列、序数の並べ替えの詳細については、「 System.Globalization.CompareOptions 」を参照してください。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。
static String IsFileURI(String path) {
return (String.Compare(path, 0, "file:", 0, 5, true)== 0); }
パスの名前は変更できない方法で比較する必要があります。これを実行するための適切なコードは次のとおりです。
static String IsFileURI(String path) {
return (String.Compare(path, 0, "file:", 0, 5, true, CultureInfo.InvariantCulture)== 0); }
使用例
[C#, C++] 文字列を比較する場合に Compare メソッドを使用しても、 ToUpper または ToLower を使用しても同じであることを次のコード例で示します。
unsafe
{
// Null terminated ASCII characters in an sbyte array
String szAsciiUpper = null;
sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
// Instruct the Garbage Collector not to move the memory
fixed(sbyte* pAsciiUpper = sbArr1)
{
szAsciiUpper = new String(pAsciiUpper);
}
String szAsciiLower = null;
sbyte[] sbArr2 = { 0x61, 0x62, 0x63, 0x00 };
// Instruct the Garbage Collector not to move the memory
fixed(sbyte* pAsciiLower = sbArr2)
{
szAsciiLower = new String(pAsciiLower, 0, sbArr2.Length);
}
// Prints "ABC abc"
Console.WriteLine(szAsciiUpper + " " + szAsciiLower);
// Compare Strings - the result is true
Console.WriteLine("The Strings are equal when capitalized ? " +
(String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper())==0?"true":"false") );
// This is the effective equivalent of another Compare method, which ignores case
Console.WriteLine("The Strings are equal when capitalized ? " +
(String.Compare(szAsciiUpper, szAsciiLower, true)==0?"true":"false") );
}
[C++]
// Null terminated ASCII characters in a simple char array
char charArray3 [4] = { 0x41, 0x42, 0x43, 0x00 };
char* pstr3 = &charArray3[0];
String* szAsciiUpper = new String(pstr3);
char charArray4 [4] = { 0x61, 0x62, 0x63, 0x00 };
char* pstr4 = &charArray4[0];
String* szAsciiLower = new String(pstr4, 0, sizeof(charArray4));
// Prints "ABC abc"
Console::WriteLine(String::Concat(szAsciiUpper," ", szAsciiLower));
// Compare Strings - the result is true
Console::WriteLine(String::Concat("The Strings are equal when capitalized ? ",
(0 == String::Compare(szAsciiUpper->ToUpper(),szAsciiLower->ToUpper())?"TRUE":"FALSE") ));
// This is the effective equivalent of another Compare method, which ignores case
Console::WriteLine(String::Concat("The Strings are equal when capitalized ? ",
(0 == String::Compare(szAsciiUpper, szAsciiLower, true)?"TRUE":"FALSE") ) );
[Visual Basic, JScript] Visual Basic および JScript のサンプルはありません。C# および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン
をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
String クラス | String メンバ | System 名前空間 | String.Compare オーバーロードの一覧 | Int32 | CompareOrdinal | CompareTo | IsPrefix