这个问题看看谁能帮给解决一下 我现在得到一个string str = "15,2,4,6,1,31";现在要像里面加入一个数字假如所加上的数字要不是和上面重复的 如"11",那么 str = "[color=#0000FF]11,15,2,4,6,1,31";[/color]要是有重复的比如说是"6" 那么 str = "[color=#0000FF]6,15,2,4,1,31";[/color]也就是将有重复的数字提到前面去这个应该怎么做呢?那位告诉我一下被! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先把字符串截取 放到一个数组中再用你要插入的数字 和array[i]比较 if(没有重复的){插入 }else{做一个类似冒泡排序的 } string value = "6,11,1"; string[] arr = value.Split(',', StringSplitOptions.RemoveEmptyEntries); List<string> lists = new List<string>(arr); string newValue = "4"; if (lists.Contains(newValue)) { lists.Remove(newValue); lists.Insert(0, newValue); } else { lists.Insert(0, newValue); } string retVal = string.Empty; foreach (string val in lists) { retVal += val; retVal += ","; } indexOf来判断有没有?有就replace(),没有就直接加上去..... string str1 = "6";string str = "15,2,4,6,1,31";string result = str1 + ("," + str + ",").Replace("," + str1 + ",", ","); Response.Write(result.Substring(0, result.Length - 1)); public string GetProcString(string newstr , string oldstr) { if (oldstr.Length > 0) { oldstr += ","; } if (oldstr.IndexOf(newstr + ",") != -1) { oldstr = oldstr.Replace(newstr + ",", ""); } oldstr = newstr + "," + oldstr; return oldstr; } oldstr = newstr + "," + oldstr;修改为:oldstr = newstr + "," + oldstr.Substring( 0,oldstr.Length-1); public string GetProcString(string newstr , string oldstr) { if (oldstr.Length > 0) { oldstr = ","+oldstr+","; } if (oldstr.IndexOf(","+newstr + ",") != -1) { oldstr = oldstr.Replace("," + newstr + ",", ","); } oldstr = newstr + "," + oldstr.Substring( 1,oldstr.Length-2); return oldstr; }那就这样处理,前后都加逗号 replace的话,就不用判断了, 找不到,自然就不会替换。:D oldstr.Substring(1,oldstr.Length-2);这里不严谨,楼主如果能确保刚开始oldstr有数据,就不用做判断了,如果oldstr可以为空,那么oldstr= newstr+","+ oldstr.Substring(1,oldstr.Length-2);就修改为if( oldstr == ""){ oldstr = newstr;}else{ oldstr= newstr+","+ oldstr.Substring(1,oldstr.Length-2);} private void button1_Click(object sender, EventArgs e) { String Str = string.Empty; Str = textBox1.Text; //这里面的值是 “1,2,3,4,5” if (Str.IndexOf(textBox2.Text) > 0) //textbox2的值就是传过来的值 { Str = Str.Replace("," + textBox2.Text, ""); } if (Str != textBox1.Text) { textBox1.Text = Str + "," + textBox1.Text; } } 求一段将网页上的某个表格绘制成图片并保存的代码 关于后台生成table的问题? 求asp.net c# 删除文件代码 asp.net错误日志处理里面怎样过滤404错误? 怎样能作一个EXE可执行文件,执行可打开网页? 如何ASP.NET从SQL的存取过程中取到output类型的值,分数不够还可以加。在线急等 问个入门问题 file field 上传,如何获取文件的扩展名 关于控件焦点纵向移动问题 那为大哥大姐有ASP.NET C# +SQL2000数据库 留言板源码给我一份好吗? 如何使用.net编写上传视频功能 难呀
再用你要插入的数字 和array[i]比较
if(没有重复的)
{插入 }
else
{做一个类似冒泡排序的 }
string[] arr = value.Split(',', StringSplitOptions.RemoveEmptyEntries); List<string> lists = new List<string>(arr);
string newValue = "4";
if (lists.Contains(newValue))
{
lists.Remove(newValue);
lists.Insert(0, newValue);
}
else
{
lists.Insert(0, newValue);
} string retVal = string.Empty;
foreach (string val in lists)
{
retVal += val;
retVal += ",";
}
string str = "15,2,4,6,1,31";
string result = str1 + ("," + str + ",").Replace("," + str1 + ",", ",");
Response.Write(result.Substring(0, result.Length - 1));
public string GetProcString(string newstr , string oldstr)
{
if (oldstr.Length > 0) { oldstr += ","; }
if (oldstr.IndexOf(newstr + ",") != -1)
{
oldstr = oldstr.Replace(newstr + ",", "");
}
oldstr = newstr + "," + oldstr;
return oldstr;
}
修改为:
oldstr = newstr + "," + oldstr.Substring( 0,oldstr.Length-1);
public string GetProcString(string newstr , string oldstr)
{
if (oldstr.Length > 0) { oldstr = ","+oldstr+","; }
if (oldstr.IndexOf(","+newstr + ",") != -1)
{
oldstr = oldstr.Replace("," + newstr + ",", ",");
}
oldstr = newstr + "," + oldstr.Substring( 1,oldstr.Length-2);
return oldstr;
}那就这样处理,前后都加逗号
replace的话,就不用判断了, 找不到,自然就不会替换。:D
那么oldstr= newstr+","+ oldstr.Substring(1,oldstr.Length-2);就修改为
if( oldstr == ""){
oldstr = newstr;
}
else
{
oldstr= newstr+","+ oldstr.Substring(1,oldstr.Length-2);
}
private void button1_Click(object sender, EventArgs e)
{
String Str = string.Empty;
Str = textBox1.Text; //这里面的值是 “1,2,3,4,5”
if (Str.IndexOf(textBox2.Text) > 0) //textbox2的值就是传过来的值
{
Str = Str.Replace("," + textBox2.Text, "");
}
if (Str != textBox1.Text)
{
textBox1.Text = Str + "," + textBox1.Text;
}
}