惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
腾讯CDC
J
Java Code Geeks
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
雷峰网
雷峰网
B
Blog RSS Feed
博客园_首页
量子位
F
Fortinet All Blogs
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog

博客园 - 三聪

流操作 如何利用.Net内置类,解析未知复杂Json对象 制作等比环切缩缩图 让window.setTimeout等支持带参数方法 利用NewID()生成随机数 创建缩略图 winform给html提供接口 IE下记住文本框的光标位置 ie下取得iframe里面内容 短网址计算 树冲突 截图片 学习Objective-C: 入门教程 asp.net本地和全局资料文件的读取方法 jquery实现点击空白的事件 Asp.net 利用Jquery Ajax传送和接收DataTable 用一个最简单方法解决asp.net页面刷新导致数据的重复提交 SOS"未将对象引用设置到对象的实例" .NET调PHP Web Service的典型例子
C#根据字节数截取字符串
三聪 · 2010-12-21 · via 博客园 - 三聪

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

        }