# 2つの文字列を比べる(String.compare) > 2 つの文字列の文字の順序にどのような違いがあるかを確認してください (ASCII エンコーディングで)。 - カテゴリ:テキスト(text) - ブロック:文字列を比べる "" と "" - URL:https://makecode.minecraft-okinawa.com/reference/text/compare/ - 公式データ取得日:2026-11-13 - 動作確認:確認待ち ## JavaScript ```javascript "".compare("") ``` ## Python ```python "".compare("") ``` ## 引数 | 名前 | 値 | 意味 | |---|---|---| | that | 文字列 | ターゲット文字列と比較する文字列 | ## 使用例 ### The string "abcdefg" is greater than "abcdefa". They are almost the same but the last letter of the second string is less than the last letter of the first one. This makes the whole second string compare as less. In blocks, the comparison of these strings looks like: ```javascript let iamGreater = false; if ("abcdefg".compare("abcdefa") > 0) { iamGreater = true; } ``` ```python iamGreater = False if "abcdefg".compare("abcdefa") > 0: iamGreater = True ``` ### See which bird is the same as a "duck". ```javascript let match = false; let bird = "duck"; if (bird.compare("crow") == 0) { match = true; } else if (bird.compare("goose") == 0) { match = true; } else if (bird.compare("duck") == 0) { match = true; } ``` ```python match = False bird = "duck" if bird.compare("crow") == 0: match = True elif bird.compare("goose") == 0: match = True elif bird.compare("duck") == 0: match = True ``` ### See how other birds compare to a "duck". ```javascript let birds = ["crow", "DUCK", "goose", "Duck", "eagle", "osprey"]; let lesserBirds = 0; let greaterBirds = 0; let ducks = 0; for (let bird of birds) { let result = bird.compare("duck"); if (result < 0) { lesserBirds++; } else if (result > 0) { greaterBirds++; } else { ducks++; } } ``` ```python birds = ["crow", "DUCK", "goose", "Duck", "eagle", "osprey"] lesserBirds = 0 greaterBirds = 0 ducks = 0 for bird in birds: result = bird.compare("duck") if result < 0: lesserBirds += 1 elif result > 0: greaterBirds += 1 else: ducks += 1 ``` ## 出典 - [MakeCode for Minecraft 公式リファレンス(String.compare)](https://minecraft.makecode.com/reference/text/compare) 執筆:鈴木孝昌(現役プロジェクトマネージャー/マイクラカップ指導者)/運営:沖縄マイクラ部 プログラミングスクール「クロスウェーブ」(https://minecraft-okinawa.com/)