how can get number in a string in c# :((((((
C#
3
Posts
3
Posters
0
Views
1
Watching
-
You can use a Regular Expression to retrieve this. The expression is as simple as
\d
-
Great resource for RegEx's useful to convert from many formats to many numeric types: [^]. Try this:
private string getID = @"'\d+'";
private Regex rxGetID;
private char[] trimQuote = new[] {'\''};
private int? htmlToInt(string match)
{
rxGetID = new Regex(getID, RegexOptions.Compiled);string result = rxGetID.Match(match).Value.Trim(trimQuote); int intResult; return (Int32.TryParse(result, out intResult)) ? intResult : (int?) null; // note the weird, but required, cast here
}
// test
private void Test()
{
rxGetID = new Regex(getID,RegexOptions.Compiled);int? result = htmlToInt("* ID='45'"); if (result != null) { // good to go } else { // throw error ? } }
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein