Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's no such function, at least none I know of. But one method would be to use a regular expression to remove everything that is not a number:</p> <pre><code>using System; using System.Text.RegularExpressions; int result = // The Convert (System) class comes in pretty handy every time // you want to convert something. Convert.ToInt32( Regex.Replace( "12ACD", // Our input "[^0-9]", // Select everything that is not in the range of 0-9 "" // Replace that with an empty string. )); </code></pre> <p>This function will yield <code>12</code> for <code>12ABC</code>, so if you need to be able to process negative numbers, you'll need a different solution. It is also not safe, if you pass it only non-digits it will yield a <code>FormatException</code>. Here is some example data:</p> <pre><code>"12ACD" =&gt; 12 "12A5" =&gt; 125 "CA12A" =&gt; 12 "-12AD" =&gt; 12 "" =&gt; FormatException "AAAA" =&gt; FormatException </code></pre> <p>A little bit more verbose but safer approach would be to use <a href="http://msdn.microsoft.com/en-us/library/f02979c7" rel="noreferrer"><code>int.TryParse()</code></a>:</p> <pre><code>using System; using System.Text.RegularExpression; public static int ConvertToInt(String input) { // Replace everything that is no a digit. String inputCleaned = Regex.Replace(input, "[^0-9]", ""); int value = 0; // Tries to parse the int, returns false on failure. if (int.TryParse(inputCleaned, out value)) { // The result from parsing can be safely returned. return value; } return 0; // Or any other default value. } </code></pre> <p>Some example data again:</p> <pre><code>"12ACD" =&gt; 12 "12A5" =&gt; 125 "CA12A" =&gt; 12 "-12AD" =&gt; 12 "" =&gt; 0 "AAAA" =&gt; 0 </code></pre> <p>Or if you only want the <em>first</em> number in the string, basically stopping at meeting something that is not a digit, we suddenly also can treat negative numbers with ease:</p> <pre><code>using System; using System.Text.RegularExpression; public static int ConvertToInt(String input) { // Matches the first numebr with or without leading minus. Match match = Regex.Match(input, "-?[0-9]+"); if (match.Success) { // No need to TryParse here, the match has to be at least // a 1-digit number. return int.Parse(match.Value); } return 0; // Or any other default value. } </code></pre> <p>And again we test it:</p> <pre><code>"12ACD" =&gt; 12 "12A5" =&gt; 12 "CA12A" =&gt; 12 "-12AD" =&gt; -12 "" =&gt; 0 "AAAA" =&gt; 0 </code></pre> <p>Overall, if we're talking about user input I would consider not accepting invalid input at all, only using <code>int.TryParse()</code> without some additional magic and on failure informing the user that the input was suboptimal (and maybe prompting again for a valid number).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload