自定义C#中的CharAt()方法

本文摘自:http://www.cnblogs.com/bianlan/archive/2012/12/10/2811228.html

Java中的string拥有CharAt()方法,C#是不拥有的,为了使用方便,我们自己可以写一个。

复制代码
using System;
 
 namespace Company{
 
     public class TestMain{
         static void Main(){
             string str = "abcdefg";
             string n_str = str.CharAt(3);
             Console.WriteLine(n_str);
         }
     }
     
     public static class CharAtExtention{
         public static string CharAt(this string s,int index){
             if((index >= s.Length)||(index<0))
                 return "";
             return s.Substring(index,1);
         }
     }
 }
复制代码

这样,直接取字符串中指定位置的字符就非常方便了。

posted @ 2016-10-27 10:35  蔡Cai  阅读(2610)  评论(0编辑  收藏  举报