C#根据字节数截取字符串与替换匹配内容的方法,举了二个例子供大家参考。
一、截取字符串
二、正确替换多个匹配内容
方法1:
private void button1_Click(object sender, EventArgs e)
{
Regex regx = new Regex(@"[image]", RegexOptions.IgnoreCase);
string content = textBox1.Text;
Match m = regx.Match(content);
while (m.Success)
{
content = content.Remove(m.Index, m.Value.Length);
content = content.Insert(m.Index, "[picture]");
m = regx.Match(content);
}
textBox2.Text = content;
}
方法2:
private void button1_Click(object sender, EventArgs e)
{
Regex regx = new Regex(@"[image]", RegexOptions.IgnoreCase);
string content = textBox1.Text;
content=regx.Replace(content, new MatchEvaluator(DoMatch));
textBox2.Text = content;
}
private string DoMatch(Match m)
{
return "[picture]";
}
function fmt(s) {
var value = s.replace(/,/g, "");
var v = value.replace(/(d+)(.d+)?/, "$1");
var d = value.replace(/(d+)(.d+)?/, "$2");
var reg = /d{4,}b/;
while (reg.test(v)) {
v = v.replace(/(d{3})b/, ',$1');
}
return v + d; ;
}