Find the number element from the string array, the string array

Source: Internet
Author: User

Find the number element from the string array, the string array

The following string array:

 string[] str = { "3","y","34","QQ","41","adsf4","7","52"};


There may be many ways to implement this requirement. The following Insus. NET is implemented using a common method:
Object-oriented: Create a Class Digit:

 

Class Digit {private int _ D; public int D {get {return _ D;} set {_ D = value ;}} public Digit (int digit) {this. _ D = digit ;}}Source Code

 

In the category, add two methods. One is to judge whether the element is a number, and the other is to override the ToString () method:

 

Public static bool TryParse (string str, out Digit digit) {digit = null; if (string. IsNullOrEmpty (str) return false; int I; if (! Int. tryParse (str, out I) return false; digit = new Digit (I); return true;} public override string ToString () {return _ D. toString ();}Source Code

 

The method has been implemented. You can test it in the console:



String [] str = {"3", "y", "34", "QQ", "41", "adsf4", "7", "52 "}; var result = new List <Digit> (); foreach (string s in str) {Digit d; if (Digit. tryParse (s, out d) result. add (d);} foreach (Digit d in result) {Console. writeLine (d. toString ());}Source Code

 

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.