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