您的位置:首页 > 其它

常用字符串处理整理

2009-02-20 16:22 176 查看
今天遇到这样一个问题,统计字符串中字符的个数。

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

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

// 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 )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: