惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
PCI Perspectives
PCI Perspectives
K
Kaspersky official blog
T
Tenable Blog
Help Net Security
Help Net Security
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
罗磊的独立博客
P
Palo Alto Networks Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
L
LangChain Blog
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
博客园_首页
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
S
Secure Thoughts
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Project Zero
Project Zero
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
V2EX
Last Week in AI
Last Week in AI
H
Heimdal Security Blog
U
Unit 42
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
SecWiki News
SecWiki News
量子位
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
S
Securelist
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
云风的 BLOG
云风的 BLOG
I
Intezer
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - Franky
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
C
CXSECURITY Database RSS Feed - CXSecurity.com

博客园 - songsoft

Js表单验证控件-02 Ajax验证 Js表单验证控件(使用方便,无需编码)-01使用说明 KindEditor编辑器For DotNet控件 模板引擎开发(三)-自定义标签的处理 Flash AS实现时钟效果(全脚本实现) 模板引擎开发(二)-值标签的处理 Jquery插件-浮动广告 模板引擎开发(一) 软件开发模型之一 万祖归宗是瀑布 关于制度化管理 关于加薪的策略 关于文档编写 蓝海与红海--有感于软件创新 李广难封--有感于团队建设 哪里是乐土?--关于团队良性循环 从酒桌上看团队建设 创造一切条件让大家“混” 百万富翁成长路线 绿有四季--关于需求沟通
字符串对比
songsoft · 2016-03-18 · via 博客园 - songsoft

工作中用到一个字符串排序,类似于windows中文件夹的排序,本来也没有什么,排序用冒泡算法就行了。

不过字符串对比,没有找到系统自带的方法,只好自己写一个。

排序采用Asciid码表,例如1与a,则a是大的;

同样字符的话,取长度最大的,例如aa与aaa,取aaa为大;如果是aab与aaa,自然是aab为最大;

/// <summary>
        /// 比较两个字符串的大值(按ascii)
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <returns></returns>
        private static string maxString(string s1, string s2)
        {
            //一些非空判断
            if (string.IsNullOrWhiteSpace(s1)) return s2;
            if (string.IsNullOrWhiteSpace(s2)) return s1;
            if (s1.Trim() == "") return s2;
            if (s2.Trim() == "") return s1;
            //对比
            int minlen = s1.Length < s2.Length ? s1.Length : s2.Length;
            char[] c1 = s1.ToCharArray();
            char[] c2 = s2.ToCharArray();
            char[] max = null;
            for (int i = 0; i < minlen; i++)
            {
                if (c1[i] != c2[i])
                {
                    max = c1[i] > c2[i] ? c1 : c2;
                    break;
                }
            }
            if (max == null) max = c1.Length > c2.Length ? c1 : c2;          
            return new string(max);
        }
        /// <summary>
        /// 比较两个字符串的小值(按ascii)
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <returns></returns>
        private static string minString(string s1, string s2)
        {
            //一些非空判断
            if (string.IsNullOrWhiteSpace(s1)) return s2;
            if (string.IsNullOrWhiteSpace(s2)) return s1;
            if (s1.Trim() == "") return s2;
            if (s2.Trim() == "") return s1;
            //对比
            int minlen = s1.Length < s2.Length ? s1.Length : s2.Length;
            char[] c1 = s1.ToCharArray();
            char[] c2 = s2.ToCharArray();
            char[] min = null;
            for (int i = 0; i < minlen; i++)
            {
                if (c1[i] != c2[i])
                {
                    min = c1[i] < c2[i] ? c1 : c2;
                    break;
                }
            }
            if (min == null) min = c1.Length < c2.Length ? c1 : c2;
            return new string(min);
        }