常用字符串处理整理

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

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

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

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 )

原文地址:https://www.cnblogs.com/tenghoo/p/1394965.html