What's Here?
- Members: 306,871
- Replies: 841,347
- Topics: 140,576
- Snippets: 4,465
- Tutorials: 1,166
- Total Online: 1,670
- Members: 106
- Guests: 1,564
|
This is a snippet to mimic VB6's Val() function. It will string all non-numeric characters from a string and return the numeric
|
Submitted By: PsychoCoder
|
|
Rating:

|
|
Views: 7,023 |
Language: C#
|
|
Last Modified: March 9, 2008 |
|
Instructions: Pass the method the string you want to strip. It will return only the numeric values in the provided string. You will need a reference to System.Text.RegularExpressions |
Snippet
//Namespace Reference
using System.Text.RegularExpressions;
// <summary>
/// Function to mimic the Val() function in legacy VB.
/// </summary>
/// <param name="str">String we want the numeric values from</param>
/// <returns>Integer</returns>
/// <remarks>
/// I chose to go with my own functions because there really
/// isn't an intrinsic method in C# to mimic the Val() function
///</remarks>
private static int Strip(string value)
{
string returnVal = string.Empty;
MatchCollection collection = Regex.Matches(value, "\\d+");
foreach (Match match in collection)
{
returnVal += match.ToString();
}
return (int)returnVal;
}
Copy & Paste
|
|
|
|