What's Here?
- Members: 244,065
- Replies: 692,841
- Topics: 113,101
- Snippets: 3,863
- Tutorials: 935
- Total Online: 1,386
- Members: 88
- Guests: 1,298
|
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: 5,127 |
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
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|