zoukankan      html  css  js  c++  java
  • 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; ;
            }
  • 相关阅读:
    在取数组的值之前,要判断数组是否为空
    Xcode更改新的Apple ID
    VS2012创建以及调用WebService
    VS添加服务引用和 Web引用的区别
    MongoDB环境搭建
    疑问
    修改导航栏标题的字体和颜色
    self.title,那么self.navigationItem.title和self.tabBarItem.title
    选中cell的时候分割线消失,如何让分割线不消失
    UITableViewCell的分割线顶头
  • 原文地址:https://www.cnblogs.com/gateluck/p/1913056.html
Copyright © 2011-2022 走看看