Compare和Equals方法 都是可以比较字符串的 public static int Compare(string strA, string strB) public static bool Equals(string a, string b) public bool Equals(string value)
easy!字符串是比长度啊。
int compare(string a, string b){ return System.Version.Parse(a) - System.Version.Parse(b); }
Version vsA = new Version(strA) Version vsB = new Version(strB) vsA.CompareTo(vsB)
string a = "2.1.45.6"; string b = "3.1.3.7"; string[] aa = a.Split('.'); string[] bb = b.Split('.'); string sum = ""; for (int i = 0; i < aa.Length; i++) { if (int.Parse(aa[i].ToString())>int.Parse(bb[i].ToString())) { sum= "a>b"; } else { sum = "b>a"; } }
if 里边加个break;``` 这个我都忘记了悲剧
string a = "2.1.45.6"; string b = "3.1.3.7"; string[] aa = a.Split('.'); string[] bb = b.Split('.'); string sum = ""; for (int i = 0; i < aa.Length; i++) { if (int.Parse(aa[i].ToString())>int.Parse(bb[i].ToString())) { sum= "a>b"; break; } else { sum = "b>a"; } }
楼上几位说的点拆分都是Version内部源码的类似操作: public int CompareTo(Version value) { if (value == null) { return 1; } if (this._Major != value._Major) { if (this._Major > value._Major) { return 1; } return -1; } else { if (this._Minor != value._Minor) { if (this._Minor > value._Minor) { return 1; } return -1; } else { if (this._Build != value._Build) { if (this._Build > value._Build) { return 1; } return -1; } else { if (this._Revision == value._Revision) { return 0; } if (this._Revision > value._Revision) { return 1; } return -1; } } } }private int _Build = -1; private int _Revision = -1; /// <summary> /// Initializes a new instance of the <see cref="T:System.Version" /> class using the specified string. /// </summary> /// <param name="version"> /// A string containing the major, minor, build, and revision numbers, where each number is delimited with a period character ('.'). /// </param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="version" /> has fewer than two components or more than four components. /// </exception> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="version" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// A major, minor, build, or revision component is less than zero. /// </exception> /// <exception cref="T:System.FormatException"> /// At least one component of <paramref name="version" /> does not parse to an integer. /// </exception> /// <exception cref="T:System.OverflowException"> /// At least one component of <paramref name="version" /> represents a number greater than <see cref="F:System.Int32.MaxValue" />. /// </exception> public Version(string version) { if (version == null) { throw new ArgumentNullException("version"); } string[] array = version.Split(new char[] { '.' }); int num = array.Length; if (num < 2 || num > 4) { throw new ArgumentException(Environment.GetResourceString("Arg_VersionString")); } this._Major = int.Parse(array[0], CultureInfo.InvariantCulture); if (this._Major < 0) { throw new ArgumentOutOfRangeException("version", Environment.GetResourceString("ArgumentOutOfRange_Version")); } this._Minor = int.Parse(array[1], CultureInfo.InvariantCulture); if (this._Minor < 0) { throw new ArgumentOutOfRangeException("version", Environment.GetResourceString("ArgumentOutOfRange_Version")); } num -= 2; if (num > 0) { this._Build = int.Parse(array[2], CultureInfo.InvariantCulture); if (this._Build < 0) { throw new ArgumentOutOfRangeException("build", Environment.GetResourceString("ArgumentOutOfRange_Version")); } num--; if (num > 0) { this._Revision = int.Parse(array[3], CultureInfo.InvariantCulture); if (this._Revision < 0) { throw new ArgumentOutOfRangeException("revision", Environment.GetResourceString("ArgumentOutOfRange_Version")); } } } }
我也写了一个,字符串拆分成数组在进行比较时,感觉上面写的几个都不是很严谨,当两者长度不一样的时候会出现异常的,会有一个数组发生越界行为如1.0.0.1.2和1.0.0.1 public int CompareString() { int num = 0; string str1 = "2.0.2.1"; string str2 = "1.0.13.8"; char[] char1 = str1.ToCharArray(); char[] char2 = str2.ToCharArray(); int i = char1.Length; int s = char2.Length; int length = 0; if (i > s) { length = s; } else { length = i; } for (int m = 0; m < length; m++) { if (char1[m] > char2[m]) { num = 1;//string str1大 break; } else if (char1[m] < char2[m]) { num = 2;//string str2大 break; } }
public static int Compare(string strA, string strB)
public static bool Equals(string a, string b)
public bool Equals(string value)
return System.Version.Parse(a) - System.Version.Parse(b);
}
a1和b2比 a2和b2比 类推 我只说个原理
Version vsB = new Version(strB)
vsA.CompareTo(vsB)
string a = "2.1.45.6";
string b = "3.1.3.7";
string[] aa = a.Split('.');
string[] bb = b.Split('.');
string sum = "";
for (int i = 0; i < aa.Length; i++)
{
if (int.Parse(aa[i].ToString())>int.Parse(bb[i].ToString()))
{
sum= "a>b";
}
else
{
sum = "b>a";
}
}
这个我都忘记了悲剧
string a = "2.1.45.6";
string b = "3.1.3.7";
string[] aa = a.Split('.');
string[] bb = b.Split('.');
string sum = "";
for (int i = 0; i < aa.Length; i++)
{
if (int.Parse(aa[i].ToString())>int.Parse(bb[i].ToString()))
{
sum= "a>b";
break;
}
else
{
sum = "b>a";
}
}
string a = "2.1.45.6";
string b = "3.1.3.7";
string[] aa = a.Split('.');
string[] bb = b.Split('.');
string sum = "";
for (int i = 0; i < aa.Length; i++)
{
if (int.Parse(aa[i].ToString()) != int.Parse(bb[i].ToString()))
{
if (int.Parse(aa[i].ToString()) > int.Parse(bb[i].ToString()))
{
sum = "a>b";
}
else
{
sum = "b>a";
}
break;
}
else
{
sum = "a=b";
}
}
} 运行了盘发觉有点问题 修改后 OK了
string b = "1.0.3.8";
string[] ss1 = a.Split('.');
string[] ss2 = b.Split('.');
for (int i = 0; i < ss1.Length; i++)
{
ss1[i] = ss1[i].PadLeft(3, '0');
ss2[i] = ss2[i].PadLeft(3, '0');
}
string r1 = string.Concat(ss1);
string r2 = string.Concat(ss2);
Response.Write(r1.CompareTo(r2));
Version v1 = new Version(s1), v2 = new Version(s2);
if (v1 > v2){
MessageBox.Show("Yes");
}
http://msdn.microsoft.com/zh-cn/library/system.version(v=vs.95).aspx
public int CompareTo(Version value)
{
if (value == null)
{
return 1;
}
if (this._Major != value._Major)
{
if (this._Major > value._Major)
{
return 1;
}
return -1;
}
else
{
if (this._Minor != value._Minor)
{
if (this._Minor > value._Minor)
{
return 1;
}
return -1;
}
else
{
if (this._Build != value._Build)
{
if (this._Build > value._Build)
{
return 1;
}
return -1;
}
else
{
if (this._Revision == value._Revision)
{
return 0;
}
if (this._Revision > value._Revision)
{
return 1;
}
return -1;
}
}
}
}private int _Build = -1;
private int _Revision = -1;
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Version" /> class using the specified string.
/// </summary>
/// <param name="version">
/// A string containing the major, minor, build, and revision numbers, where each number is delimited with a period character ('.').
/// </param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="version" /> has fewer than two components or more than four components.
/// </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="version" /> is null.
/// </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// A major, minor, build, or revision component is less than zero.
/// </exception>
/// <exception cref="T:System.FormatException">
/// At least one component of <paramref name="version" /> does not parse to an integer.
/// </exception>
/// <exception cref="T:System.OverflowException">
/// At least one component of <paramref name="version" /> represents a number greater than <see cref="F:System.Int32.MaxValue" />.
/// </exception>
public Version(string version)
{
if (version == null)
{
throw new ArgumentNullException("version");
}
string[] array = version.Split(new char[]
{
'.'
});
int num = array.Length;
if (num < 2 || num > 4)
{
throw new ArgumentException(Environment.GetResourceString("Arg_VersionString"));
}
this._Major = int.Parse(array[0], CultureInfo.InvariantCulture);
if (this._Major < 0)
{
throw new ArgumentOutOfRangeException("version", Environment.GetResourceString("ArgumentOutOfRange_Version"));
}
this._Minor = int.Parse(array[1], CultureInfo.InvariantCulture);
if (this._Minor < 0)
{
throw new ArgumentOutOfRangeException("version", Environment.GetResourceString("ArgumentOutOfRange_Version"));
}
num -= 2;
if (num > 0)
{
this._Build = int.Parse(array[2], CultureInfo.InvariantCulture);
if (this._Build < 0)
{
throw new ArgumentOutOfRangeException("build", Environment.GetResourceString("ArgumentOutOfRange_Version"));
}
num--;
if (num > 0)
{
this._Revision = int.Parse(array[3], CultureInfo.InvariantCulture);
if (this._Revision < 0)
{
throw new ArgumentOutOfRangeException("revision", Environment.GetResourceString("ArgumentOutOfRange_Version"));
}
}
}
}
{
int num = 0;
string str1 = "2.0.2.1";
string str2 = "1.0.13.8"; char[] char1 = str1.ToCharArray();
char[] char2 = str2.ToCharArray();
int i = char1.Length;
int s = char2.Length;
int length = 0;
if (i > s)
{
length = s;
}
else
{
length = i;
}
for (int m = 0; m < length; m++)
{
if (char1[m] > char2[m])
{
num = 1;//string str1大
break;
}
else if (char1[m] < char2[m])
{
num = 2;//string str2大
break;
}
}
return num;
}
还有一种情况没有写出来,就是1.0.0.1.2和1.0.0.1的这种情况自己写吧!