C#根据字节数截取字符串
生活随笔
收集整理的這篇文章主要介紹了
C#根据字节数截取字符串
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?? ? ? ?///?<summary>
????????///?按最大字節(jié)數(shù),截取字符串
????????///?</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;
}
{
????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]";
}
????????///?按最大字節(jié)數(shù),截取字符串
????????///?</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;
????????}
?
正確替換多個匹配內(nèi)容
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; ;}?
轉(zhuǎn)載于:https://www.cnblogs.com/gateluck/archive/2010/12/21/1913056.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的C#根据字节数截取字符串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: new/delete和malloc/fr
- 下一篇: makefile格式-实践一