C#:String类型中的CharAt 方法


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);
         }
     }
 }

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM