C #: CharAt in the String type

Source: Internet
Author: User

Today, I do not know which tendon is wrong. The disordered memory shows that the string in C # has the CharAt () method. Alas, I have to say that I have substituted this usage in java into C.

After the proof is absent, the head's thinking is severely frustrated. I am stubborn in doing things, so I won't give up without reaching my goal. Since there is no CharAt () method in C #, I will compile it for him.

C # has one extension method.

I used this method to compile a CharAt () method for string. The Code is as follows:

1 using System;
2
3 namespace zuo_Company {
4
5 public class TestMain {
6 static void Main (){
7 string str = "abcdefg ";
8 string n_str = str. CharAt (str. Length-1 );
9 Console. WriteLine (n_str );
10}
11}
12
13 public static class CharAtExtention {
14 public static string CharAt (this string s, int index ){
15 if (index> = s. Length) | (index <0 ))
16 return "";
17 return s. Substring (index, 1 );
18}
19}
20}

Some possible things were taken into consideration when making this method. For example, if the length of the entire string is exceeded, the extraction operation cannot be performed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.