string str="".Trim();Trim() 是string 类型里的方法,我现在想从写一下string 类型,加入一个自己定义的方法,
让整个项目的string 类型都可以点出这个方法,就可以本字符串截取前六位private string ShowLength()
{
 return  this.Substring(0,6);
}该怎么做呢
string重写长度

解决方案 »

  1.   

    public static StringHelper
    {
    private static string ShowLength(this string str)
    {
     return  str.Substring(0,6);
    }
    }
      

  2.   

    public static StringHelper
    {
    public static string ShowLength(this string str)
    {
     return  str.Length<6?str:str.Substring(0,6);
    }
    }
      

  3.   


    这样好像不行吧,我要 string str="123456789".ShowLength();
    str 就得到  123456
      

  4.   


    再请教一下
    public static StringHelper这个东东你放在哪里呢?string类型好像是从元数据,不能修改呢
      

  5.   

    是public static class StringHelper,掉了个class
    新建个类放进去就行