string s="#我爱中国china#士大夫方式大幅士#大夫#sdf";
如何把把s中替换为s1="#我爱中国china#";
s2="士大夫方式大幅士";
s3="#大夫#";
s4="sdf";
即是我要对"#"号之间的字符做处理对s1,及s3处理后
再组成s=s1+s2+s3+s4;
如何把把s中替换为s1="#我爱中国china#";
s2="士大夫方式大幅士";
s3="#大夫#";
s4="sdf";
即是我要对"#"号之间的字符做处理对s1,及s3处理后
再组成s=s1+s2+s3+s4;
如果需要这个要在split后的数组中自己加了
Regex reg = new Regex(@"#[^#]*#|[^#]+");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}
string result =string.Join("", s.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries));
修改下 string test = "#我爱中国china#士大夫方式大幅士#大夫#sdf";
Regex reg = new Regex(@"#[^#]*#|[^#]+");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
if (m.Value.ToString().Contains("#"))
{
this.Label1.Text += GetUrlCode(m.Value) ;
}
else
{
this.Label1.Text += m.Value ;
}自己写的方法
string txt = "#我爱中国china#士大夫方式大幅士#大夫#sdf"; string[] txt2 = txt.Split('#');
StringBuilder sb = new StringBuilder();
if (txt2.Length > 2)
{
for (int i = 0; i < txt2.Length; i++)
{ if (i % 2 == 1)
{
sb = sb.Append(GetUrlCode(txt2[i]));
}
else
{
sb = sb.Append(txt2[i]);
} }
}
else
{
sb = sb.Append(txt);
}
string txt = "#我爱中国china#士大夫方式大幅士#大夫sdf"; string[] txt2 = txt.Split('#');
StringBuilder sb = new StringBuilder();
if (txt2.Length > 2)
{
for (int i = 0; i < txt2.Length; i++)
{ if (i % 2 == 1&&i< txt2.Length-1)
{
sb = sb.Append(GetUrlCode(txt2[i]));
}
else
{
sb = sb.Append(txt2[i]);
} }
}
else
{
sb = sb.Append(txt);
}