最近面试,总是失败,就的天天啃书了数据结构,设计模式,C#高级,javascript高级样样都的看,现在的企业简直就是要全能的嘛!
哎.....
上代码,欢迎拍砖,性能需优化...
哎.....
上代码,欢迎拍砖,性能需优化...
解决方案 »
- c#如何实现欢迎窗口
- C# 死循环问题
- 为什么绑定在dataGridView上点一次,dataGridView没有保留上次的内容
- 关于查找保存后的网页找并上传网页中图片的问题,急求图片自动查找并上传问题
- winform自制DataGridViewCell
- vs.net 2005 问题!
- 怎样做手机界面上的信号条显示?UP给分
- 【急求】 C# 模拟登录网站,本人Web很菜
- 实现Windows 服务的问题
- 高手请赐教:从html页中析出超链接,为什么这段源程序只能解出老外的,对中文的网也没用???
- C#换肤技术-如何更换背景图片,并且使窗体上所有空间都还能显示
- C# 在使用FindWindowEx的参数使用详解。
public override string ToString()
{
StringBuilder str = new StringBuilder();
foreach (var item in this)
{
str.Append(item);
}
return str.ToString();
} #region IEnumerable<string> 成员 public IEnumerator<char> GetEnumerator()
{
for (int i = 0; i < this.GetLength(); i++)
{
yield return this[i];
}
} #endregion #region IEnumerable 成员 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
} #endregion
public class StringDS : IEnumerable<char>
{ private char[] data; /// <summary>
/// 创建索引
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public char this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
} /// <summary>
/// char数组构造
/// </summary>
/// <param name="arr"></param>
public StringDS(char[] arr)
{
data = new char[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
data[i] = arr[i];
} } /// <summary>
/// StringDS构造
/// </summary>
/// <param name="str"></param>
public StringDS(StringDS str)
{
data = new char[str.GetLength()];
for (int i = 0; i < str.data.Length; i++)
{
data[i] = str.data[i];
}
} /// <summary>
/// 长度构造
/// </summary>
/// <param name="len"></param>
public StringDS(int len)
{
data = new char[len];
}
/// <summary>
/// 字符串长度
/// </summary>
/// <returns></returns>
public int GetLength()
{
return data.Length;
}
/// <summary>
/// 两个字符串是否相等
/// </summary>
/// <param name="s">要比较的串</param>
/// <returns></returns>
public bool Compare(StringDS s)
{
bool flag=true ;
if (this.GetLength() != s.GetLength())
{
flag= false;
}
else
{
for (int i = 0; i < this.GetLength(); i++)
{
if (this[i] != s[i])
{
flag = false;
break;
}
}
}
return flag; }
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="len">截取的长度</param>
/// <param name="startIndex">开始截取位置</param>
/// <returns></returns>
public StringDS SubString(int len, int startIndex = 1)
{
if (startIndex < 0 || startIndex > this.GetLength() - 1||len<0||len>this.GetLength()-startIndex)
{
Console.WriteLine("Error");
return null;
}
StringDS s = new StringDS(len);
for (int i = 0; i < len; i++)
{
s[i] = this[i + startIndex - 1];
}
return s;
}
/// <summary>
/// 字符串替换
/// </summary>
/// <param name="s">原字符串</param>
/// <param name="rs">替换字符串</param>
/// <returns></returns>
public StringDS Replace(StringDS s, StringDS rs)
{
if (this.GetLength() < s.GetLength())
{
Console.WriteLine("Error");
return null;
}
//先找s在原串中出现的次数
int index=0;
int slen = s.GetLength(); //被替换串的长度
int rlen = rs.GetLength();//替换串的长度
int length = this.GetLength(); //原串长度
bool flag=false ;
for (int i = 0; i < length; i++)
{
flag = true;
for (int j = 0; j < slen; j++)
{
if (this[i + j] != s[j])
{
flag = false;
break;
}
}
if (flag)
{
i = i + slen;
index = index + 1;
}
}
//得到原串中s出现的次数就能得到替换后字符串的长度=原串长+(替换串长-被替换串长)*出现次数
StringDS ds = new StringDS(this.GetLength() + ((rs.GetLength() - slen) * index));
int dslen=ds.GetLength();//替换后串长
index = 0; //index置0,控制新串小标
for (int i = 0; i < length-1; i++)
{
flag = true;
for (int j = 0; j < slen; j++) //和被替换串比较
{
if (this[i + j] != s[j])
{
flag = false;
break;
}
}
if (flag) //和被替换串匹配
{
for (int m = 0; m < rlen; m++) //将替换串赋给新串
{
ds[index + m] = rs[m];
}
i = i + slen-1 ;//将原串的下标加上被替换串的长度减1
index = index + rlen-1; //将新串下标加替换串长度减1
}
else //不匹配则将原串值直接给新串
{
ds[index] = this[i];
}
index = index + 1;
}
return ds;
}
/// <summary>
/// 追加串
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public StringDS Append(StringDS s)
{
int length = this.GetLength(); //原串长
int slength = s.GetLength();//追加串长
if (length == 0)
{
return s;
}
if (slength == 0)
{
return this;
}
int newLength = length + slength;
StringDS newStr = new StringDS(newLength); //构造新串
for (int i = 0; i < length; i++) //将原串赋给新串
{
newStr[i] = this[i];
}
int index = length - 1;
for (int i = 0; i < slength; i++) //将追加串赋给新串
{
newStr[++index] = s[i];
}
return newStr;
}
}
/// <summary>
/// 插入新串
/// </summary>
/// <param name="s">插入的串</param>
/// <param name="index">插入的位置</param>
/// <returns></returns>
public StringDS Insert(StringDS s,int index)
{
int length = this.GetLength(); //原串长
int slength = s.GetLength(); //插入串长
if (length == 0||index>length||index<0)
{
Console.WriteLine("Error");
return null;
}
StringDS newStr = new StringDS(length+slength); //构造新串
//1.将原串的0到插入的位置赋给新串
//2.将插入串赋给新串
//3.将原串插入位置后的赋给新串
for (int i = 0; i < index; i++)
{
newStr[i] = this[i];
}
for (int i = index; i < slength; i++)
{
newStr[i] = s[i];
}
for (int i = index; i < length; i++)
{
newStr[i + slength] = this[i];
}
return newStr;
}
/// <summary>
/// 移除
/// </summary>
/// <param name="startIndex">开始位置</param>
/// <param name="endIndex">结束位置</param>
/// <returns></returns>
public StringDS Remove(int startIndex,int endIndex)
{
int length = this.GetLength(); //原串长
if (length == 0 || startIndex <= 0 || endIndex <= 0 || (startIndex >= endIndex))
{
Console.WriteLine("Error");
return null;
}
int newLength = length - endIndex + startIndex;
StringDS newStr = new StringDS(newLength); //构造新串
//1.将原串0到删除的startIndex开始位置赋给新串
//2.将原串删除结束位置endIndex之后赋给新串
for (int i = 0; i < startIndex; i++)
{
newStr[i] = this[i];
}
for (int i = endIndex; i < length; i++)
{
newStr[i] = this[i];
}
return newStr;
}
/// <summary>
/// 分割串
/// </summary>
/// <param name="s">以什么分割</param>
/// <returns></returns>
public StringDS[] Split(StringDS s)
{
StringDS ds = this.Append(new StringDS(s)); //在末尾追加一个分割串,后面处理方便
int length = ds.GetLength();
int slength = s.GetLength();
bool flag = false;
int index = 0;
for (int i = 0; i < length ; i++)
{ flag = true;
for (int j = 0; j < slength; j++) //和分割串比较
{
if (ds[i + j] != s[j])
{
flag = false;
break;
}
}
if (flag)
{
index = index + 1;
}
}
StringDS[] arr = new StringDS[index]; //初始化分割后数组
StringDS tempstr;
index=0;//分割数组计数器
int tempindex = 0;//计算每次分割在原字符串中的位置
int tempi=0; //没一次分割串处理计数器
for (int i = 0; i < length; i++)
{
flag = true;
for (int j = 0; j < slength; j++) //和分割串比较
{
if (ds[i + j] != s[j])
{
flag = false;
break;
}
}
if (flag) //和分割串匹配
{
tempi = 0;
tempstr = new StringDS(i - tempindex); //构造临时StringDS存放串 在放入数组
for (int m = tempindex; m < i; m++)
{
tempstr[tempi] = this[m];
++tempi;
}
tempindex = i + slength;
arr[index] = tempstr;
++index;
}
}
return arr;
}
1.这里发言,表示您接受了CSDN社区的用户行为准则。
2.请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。
3.转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。
2.请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。
3.转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。
这些字符串操作,.net自己有吗? 如果没有,我就拷贝下来,以后就可以省事了。
.net 肯定有啊 真是的
请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者
用过.Net的string吗?用过的话 你就知道这些最基本的方法,虽然写的效率不高,但是有这么一种方法...
我觉得不是微软没实力去扩充这些东西,只是觉得没必要。而您提供的好多方法其实可以用扩展方法的形式直接挂靠在string上就可以了,至于其它的,也可以挂靠在stringbuilder上。就像我们现在看到的好多三方控件一样,微软之所以不做了,是不想和下游厂商争小利。其实人就是这样:人家都做了,你说人家垄断,人家不做吧,又成了别人说三道四的理由了