截取前十个字符

来源:互联网 发布:软件性能测试 编辑:程序博客网 时间:2024/04/29 20:16
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace TESTDEMO{    class Program    {        static void Main(string[] args)        {            string characters = "惺惺相惜";            Console.WriteLine(SubStringTen(characters));            Console.Read();        }        /// <summary>        /// 截取前十个字符        /// </summary>        /// <param name="characters"></param>        /// <returns></returns>        static string SubStringTen(string characters)        {            MatchCollection matches = Regex.Matches(characters, ".");            int length = matches.Count > 10 ? 10 : matches.Count;            StringBuilder str = new StringBuilder();            for (int i = 0; i < length; i++)            {                str.Append(matches[i].Value);            }            return str.ToString();        }    }}