Search for numeric elements from the string array.
A few days ago Insus. NET has written a "from the string array to find the number of elements" http://www.cnblogs.com/insus/p/8001026.html
And its Extension Article C # yield keyword use http://www.cnblogs.com/insus/p/8003809.html
It is to write a category to process and collect numeric elements.
Development Programs, the solution is not the only one. There are more than one method for implementing the same function. The following Insus. NET is implemented using another method, which is counted as the consolidation and mastery of basic knowledge.
Refer to the following code:
For the above lines of code from #21 to #30, you can use the yield method to return the cyclic result:
Class Ak {private string [] _ stringArray; public Ak (string [] stringArray) {this. _ stringArray = stringArray;} public IEnumerable <Digit> Result () {// var result = new List <Digit> (); foreach (string s in _ stringArray) {string pattern = "^ [0-9]"; Regex regex = new Regex (pattern); if (regex. isMatch (s) yield return new Digit (Convert. toInt32 (s);} // return result;} public void Output () {foreach (Digit d in Result () {Console. writeLine (d. toString ());}}}Source Code
Running result:
The result is the same as that of the previous custom method.
For future convenience and maintenance, you can write the regular expression verification code as a method or an extension method. You can directly use this method when the program requires regular expression verification. To achieve the three elements of object-oriented, encapsulation:
Use regular expressions to create an extension method:
Public static bool Match (this string value, string pattern) {Regex regex = new Regex (pattern); return regex. IsMatch (value );}Source Code
Then, the program code can eventually become like this: