常用字符串处理整理

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

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

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

 

ContractedBlock.gif ExpandedBlockStart.gif 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 + "");
                }
            }
        }

 

结果:

2009022016232480.gif

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

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

 

ContractedBlock.gif ExpandedBlockStart.gif 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());
        }

结果:

2009022016464318.gif

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

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

 

ContractedBlock.gif ExpandedBlockStart.gif Code
// splitStr("1.aa|2.bb|3.cc=4.dd|5.ee|6.ff=7.dd|8.ee|9.ff"); 
private void splitStr(string str)
        {
            StringBuilder sb 
= new StringBuilder();
            
string[] arr=str.Split('=');
            
int length=arr.Length;
            
string[,] data = new string[length,3];
            
for (int i = 0; i < arr.Length; i++)
            {
                
string[] arr1 = arr[i].Split('|');
                
for (int j = 0; j < arr1.Length; j++)
                {
                    
string[] arr2 = arr1[j].Split('.');
                     data[i,j] 
= arr2[0];
                }
            }
            
for (int i = 0; i < data.GetLength(0); i++)
            {
                
if (i != 0)
                    sb.Append(
" or ");
                sb.Append(
" (");
                sb.Append(
" class1="+data[i,0]);
                sb.Append(
" and class2=" + data[i, 1]);
                sb.Append(
" and class3=" + data[i, 2]);
                sb.Append(
" ) ");
            }
            
string aa=sb.ToString();
        }

结果:

( 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 )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值