Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how can get number in a string in c# :((((((

how can get number in a string in c# :((((((

Scheduled Pinned Locked Moved C#
csharpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mhd sbt
    wrote on last edited by
    #1

    hi how can i get number from a string like this :

    just using c# thankyou

    P B 2 Replies Last reply
    0
    • M mhd sbt

      hi how can i get number from a string like this :

      just using c# thankyou

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You can use a Regular Expression to retrieve this. The expression is as simple as \d

      1 Reply Last reply
      0
      • M mhd sbt

        hi how can i get number from a string like this :

        just using c# thankyou

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups