在 C# 中按字典順序比較兩個字串
如需在 C# 中比較字串,請使用比較()方法。它比較兩個字串並返回以下整數 -
If str1 is less than str2, it returns -1. If str1 is equal to str2, it returns 0. If str1 is greater than str2, it returns 1.
在字串中設定兩個字串。compare()方法並比較它們 -
string.Compare(string1, string2);
示例
您可以嘗試執行以下程式碼來比較 C# 中的兩個字串。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
class MyApplication {
static void Main(string[] args) {
string string1 = null;
string string2 = null;
string1 = "amit";
string2 = "Amit";
int myOutput = 0;
myOutput = string.Compare(string1, string2);
Console.WriteLine(myOutput.ToString());
Console.ReadLine();
}
}
}輸出
-1
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP