I am new to asp.net can you help me to find out how to find and extract the numbers from string in Asp.net.
2 Answers
Method 1:
String str = "udsdf34dfd78"; /*any string*/
String strNumber = "";
Regex regex=new Regex(@"\d");
foreach (Match m in regex.Matches(str))
strNumber += m.Value;
Method 2:
String str = "udsdf34dfd78"; /*any string*/
String strNumber = "";
foreach (Char c in str)
if (Char.IsDigit(c))
strNumber += c.ToString();
Comments
string output = new string(input.Where(char.IsDigit).ToArray());
4 Comments
Damith
@ShahroozJefriㇱ depends on length of the string
Jeroen Vannevel
A regex isn't free either. You won't notice a thing for both methods, you're too focused on premature optimization.