• 常用字符串处理整理


    今天遇到这样一个问题,统计字符串中字符的个数。

    写了一个算法基本实现了这个功能,只是不知是否有性能更高的算法。

     感兴趣的一起试试。方法比较简单,就不写注释了,相信大家能够看懂。

    Code
            public static void run()
            {
                
    string str = "abcdeaadfec53543543253@!#@!#$$@@!@#@!#!@3";
                
    bool flag; 
                
    int count;
                
    for (int i = 0; i < str.Length; i++)
                {
                    flag 
    = true;
                    count
    =0;
                    
    for (int k = 0; k < i; k++)
                    {
                        
    if (str[i] == str[k]) flag = false;
                    }
                    
    if (flag == true)
                    {
                        
    for (int j = 0; j < str.Length; j++)
                        {
                            
    if (str[i] == str[j])
                            {
                                count
    ++;
                            }
                        }
                        Console.WriteLine(str[i] 
    + "出现" + count + "");
                    }
                }
            }

    结果:

    ------10分钟后补充---------------------

    索性再贴一个反转字符串的方法,以后想到别的字符串处理统统贴到这里。O(∩_∩)O哈哈~

    Code
            //反转字符串
            public static void run2()
            {
                
    string str = "abcdeaadfec53543543253@!#@!#$$@@!@#@!#!@3gz,l;_+)(";
                
    char[] chr = new char[str.Length];
                StringBuilder sb 
    = new StringBuilder();
                
    for (int i = 0; i < str.Length; i++)
                {
                    chr[i] 
    = str[str.Length - 1 - i];
                }
                
    foreach (var c in chr)
                {
                    sb.Append(c);
                }
                Console.WriteLine(sb.ToString());
            }

    结果:

    ------20090226补充---------------------

    拆分这样的串:"1.aa|2.bb|3.cc=4.dd|5.ee|6.ff=7.dd|8.ee|9.ff"

    Code

    结果:

    ( class1=1 and class2=2 and class3=3 )  or 
    ( class1=4 and class2=5 and class3=6 )  or 
    ( class1=7 and class2=8 and class3=9 )

  • 相关阅读:
    WinowsXP 任务栏无法显示当前运行程序图标
    日志记录组件[Log4net]详细介绍(转)
    桌面上的IE图标变成了快捷方式那种图标 怎么还原回来
    面试必须要知道的SQL语法,语句(转载)
    兼容 火狐 IE 的JS时间控件 任意格式 年月日时分秒
    Nagios远程监控软件的安装与配置详解(1)
    linux集群 负载均衡实验笔记
    PHPB2B 模板 标签
    PHP 去除 HTML 回车 换行 空格
    OpenX参考网址
  • 原文地址:https://www.cnblogs.com/tenghoo/p/1394965.html
Copyright © 2020-2023  润新知