Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A fairly simple-minded approach would be to use a regular expression. Depending on which type of phone numbers you're accepting, you could write a regular expression that looks for the digits (for US-only, you know there can be 7 or 10 total - maybe with a leading '1') and potential separators between them (period, dash, parens, spaces, etc.).</p> <p>Once you run the match against the regex, you'll need to write the logic to determine what you actually got and format it from there.</p> <p>EDIT: Just wanted to add a very basic example (by no means is this going to work for all of the examples you posted above). Geoff's suggestion of stripping non-numeric characters might help out a bit depending on how you write your regex.</p> <pre><code>Regex regex = new Regex(@"(?&lt;areaCode&gt;([\d]{3}))?[\s.-]?(?&lt;leadingThree&gt;([\d]{3}))[\s.-]?(?&lt;lastFour&gt;([\d]{4}))[x]?(?&lt;extension&gt;[\d]{1,})?"); string phoneNumber = "701 123-4567x324"; Match phoneNumberMatch = regex.Match(phoneNumber); if(phoneNumberMatch.Success) { if (phoneNumberMatch.Groups["areaCode"].Success) { Console.WriteLine(phoneNumberMatch.Groups["areaCode"].Value); } if (phoneNumberMatch.Groups["leadingThree"].Success) { Console.WriteLine(phoneNumberMatch.Groups["leadingThree"].Value); } if (phoneNumberMatch.Groups["lastFour"].Success) { Console.WriteLine(phoneNumberMatch.Groups["lastFour"].Value); } if (phoneNumberMatch.Groups["extension"].Success) { Console.WriteLine(phoneNumberMatch.Groups["extension"].Value); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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