常用字符串处理整理
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 )
写了一个算法基本实现了这个功能,只是不知是否有性能更高的算法。
感兴趣的一起试试。方法比较简单,就不写注释了,相信大家能够看懂。
// 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 )
相关文章推荐
- php中常用字符串处理代码片段整理
- PHP 常用字符串,数组处理函数整理(一)
- 今天项目用到了字符串处理,简单整理下几个常用
- 常用字符串处理整理
- php中常用字符串处理代码片段整理
- ANSI C 库最常用的几个处理字符串的函数
- php常用字符串处理函数实例分析
- php常用字符串处理函数实例分析
- 几个常用的JavaScript字符串处理函数 - split()、join()、substring()和indexOf()
- Delphi中常用字符串处理函数
- Golang 常用字符串处理方法汇总
- string.h中常用字符串处理函数
- C字符串常用处理函数
- SQL Server处理字符串常用的函数
- 常用处理字符串的SQL函数
- 总结的php10个常用的处理字符串的函数事例
- Shell常用处理字符串方法(备查)
- 几个常用的JavaScript字符串处理函数 – split()、join()、substring()和indexOf()
- C#中常用处理字符串的方法