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

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

相关文章:

  • 2022-12-23
  • 2021-10-18
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
猜你喜欢
  • 2021-08-21
  • 2022-12-23
  • 2021-07-26
  • 2021-08-02
  • 2022-12-23
  • 2021-07-16
  • 2022-12-23
相关资源
相似解决方案