using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test_Str : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
Response.Write(replaceOne("1,2,3,4", "1") + "<br/>");//在头部
Response.Write(replaceOne("1,2,3,4", "2") + "<br/>");//在中间
Response.Write(replaceOne("1,2,3,4", "4") + "<br/>");//在尾部
Response.Write(replaceOne("121", "1") + "---<br/>");//不能把121中的1替换
Response.Write(replaceOne("1,121,1,1", "1") + "---<br/>");
Response.Write(replaceOne("1", "2") + "---<br/>");//只有一个
}
/// <summary>
/// 要实现的是将"1,2,12,13"这样的字符串中的某个字符串替换为空
/// 比如如果替换"1"则替换之后为"2,12,13"不能把12中的1也替换了
/// 如果替换12则替换后为"1,2,13"
/// </summary>
/// <param name="arrStr">数组型字符串</param>
/// <param name="one">要替换掉的字符串</param>
/// <returns></returns>
private string replaceOne(string arrStr, string one)
{
//如果长度为1,简单替换
if (arrStr.Length == 1)
{
return arrStr.Replace(one, "");
}
//如果以要替换的字符串结尾"1,2,one"
if (arrStr.EndsWith("," + one))
{
arrStr = arrStr.Replace("," + one, "");
}
//字符串头部与中间,都可以这样替换
arrStr = arrStr.Replace(one + ",", "");
return arrStr;
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test_Str : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
Response.Write(replaceOne("1,2,3,4", "1") + "<br/>");//在头部
Response.Write(replaceOne("1,2,3,4", "2") + "<br/>");//在中间
Response.Write(replaceOne("1,2,3,4", "4") + "<br/>");//在尾部
Response.Write(replaceOne("121", "1") + "---<br/>");//不能把121中的1替换
Response.Write(replaceOne("1,121,1,1", "1") + "---<br/>");
Response.Write(replaceOne("1", "2") + "---<br/>");//只有一个
}
/// <summary>
/// 要实现的是将"1,2,12,13"这样的字符串中的某个字符串替换为空
/// 比如如果替换"1"则替换之后为"2,12,13"不能把12中的1也替换了
/// 如果替换12则替换后为"1,2,13"
/// </summary>
/// <param name="arrStr">数组型字符串</param>
/// <param name="one">要替换掉的字符串</param>
/// <returns></returns>
private string replaceOne(string arrStr, string one)
{
//如果长度为1,简单替换
if (arrStr.Length == 1)
{
return arrStr.Replace(one, "");
}
//如果以要替换的字符串结尾"1,2,one"
if (arrStr.EndsWith("," + one))
{
arrStr = arrStr.Replace("," + one, "");
}
//字符串头部与中间,都可以这样替换
arrStr = arrStr.Replace(one + ",", "");
return arrStr;
}
}