C#根据字节数截取字符串

        /// <summary>
        
/// 按最大字节数,截取字符串
        
/// </summary>
        
/// <param name="value">要截取的字符串</param>
        
/// <param name="length">最大长度</param>
        
/// <returns></returns>
        private string Intercept(string value, int length)
        {
            
if (value.Length * 2 <= length)
            {
                
return value;
            }
            
string newvalue;
            
for (int i = length / 2; i < value.Length; i++)
            {
                newvalue 
= value.Substring(0, i);
                
if (Encoding.Default.GetByteCount(newvalue) > length)
                {
                    
return value.Substring(0, i - 1);
                }
            }
            
return value;
        }

正确替换多个匹配内容


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;
}


 方法二:

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; ;
        }
原文地址:https://www.cnblogs.com/gateluck/p/1913056.html