C#基础-053 两个字符串比较方法(3种)
来源:互联网 发布:2020年庚子大难知乎 编辑:程序博客网 时间:2024/06/05 00:46
static void Main(string[] args) { Console.WriteLine( Mycompare_03("sasgb", "sdsdsdgg") ); } static int Mycompare_01(string str1, string str2)//自己实现的3种方法MyCompare { if (str1.Length > str2.Length) { for (int i = 0; i < str1.Length - str2.Length + 1; i++) { for (int j = 0; j < str2.Length - i; j++) { if (str1[j] > str2[i + j]) { return 1; } if (str1[j] < str2[i + j]) { return -1; } } } } else { for (int i = 0; i < str2.Length - str1.Length + 1; i++) { for (int j = 0; j < str1.Length - i; j++) { if (str2[j] > str1[i + j]) { return -1; } if (str2[j] < str1[i + j]) { return 1; } } } } return 0; } static int Mycompare_02(string str1, string str2)//最优 { // abc abc for (int i = 0; i < str1.Length; i++) { //如果str2比str1长的情况 if (i > str2.Length - 1) { return 1; } if (str1[i] > str2[i]) { return 1; } else if (str1[i] < str2[i]) { return -1; } } //str1的长度小于或等于str2的长度 return str1.Length == str2.Length ? 0 : -1; } static int Mycompare_03(string str1, string str2) { //abc abc for (int i = 0; i < str1.Length; i++) { //如果str2比str1长的情况 if (i > str2.Length - 1) { return 1; } if (str1[i] > str2[i]) { return 1; } else if (str1[i] < str2[i]) { return -1; } } return Math.Abs(str1.Length - str2.Length)==0 ? 0 : (str1.Length - str2.Length) / Math.Abs(str1.Length - str2.Length); }
阅读全文
0 0
- C#基础-053 两个字符串比较方法(3种)
- c#比较两个字符串相等的方法。
- C#控制台基础 比较两个字符串,无视大小写
- C#字符串比较方法
- C#字符串比较方法
- 比较两个字符串空,两种方法性能比较
- [C#基础]字符串方法
- C#输出字符串的两个方法
- C#中比较字符串的方法
- C#各种判断空字符串方法比较
- C#字符串转换比较(常用方法) 2011.3
- 用C#比较字符串有多种方法
- C#中比较字符串的方法
- C#下比较两个等长字符串是否含有完全相同字符(忽略字符顺序)
- C#中使用Equals()函数比较两个字符串是否相同
- 比较两个字符串的大小(两种方法,重点在介绍数组元素可以作为函数的实参)
- C#基础-006(4)if练习 比较控制台输入的两个数字的大小
- javascript 比较两个字符串
- HDU 1083:Courses
- hdu 6085(bitset优化)
- IMP-00013: 只有 DBA 才能导入由其他 DBA 导出的文件
- 字符编码笔记:ASCII,Unicode和UTF-8
- axure实现多人协作画图
- C#基础-053 两个字符串比较方法(3种)
- 秋凉
- 写规约程序过程中的思考
- Redis集群方案及实现
- Temporary Post Used For Theme Detection (0ecb8fae-2539-45f9-a310-9e6b31cb889d
- ubuntu 修改root密码
- java文件写入
- 硬件支持openwrt系统第一步
- N皇后问题(DFS)