Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string value, that is string str="b(2)"
I need that integer value only, that mean i need a output is int value = 2
How should i make it?
Posted

Use regular expressions. Try
C#
input = Regex.Replace(input, "[^0-9]+", string.Empty);
 
Share this answer
 
v2
Comments
MVK_VIJI 6-Oct-15 12:30pm    
But this return a string value only, i need integer value,
Maciej Los 6-Oct-15 12:44pm    
So, convert it to integer: int intVal = (int)input;
MVK_VIJI 6-Oct-15 12:52pm    
It gives error, cannot convert type string to int
Abhinav S 6-Oct-15 12:53pm    
Use Int32.Parse
F-ES Sitecore 6-Oct-15 12:51pm    
If you don't know how to convert a string to an int then I suggest you get a book on c# and start to learn the basics.

int.TryParse
I usually try to create, general-purpose, re-usable, "small" code-tools in situations like this:
C#
private static Regex GetInt = new Regex(@"\d+");

private bool StringToInt32(string str, ref int result)
{       
    return Int32.TryParse(GetInt.Match(str).Value, out result);
} 

// Example of use:

// int result = 0;
// string aString = "b(2)";

// if (StringToInt32(aString, ref result))
// {
//    int x = 33;
// }
 
Share this answer
 
v2
Comments
Maciej Los 7-Oct-15 6:42am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900