PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 20, 2022

[FIXED] How to extract integer values from inside a string

 July 20, 2022     c#, integer, split, string     No comments   

Issue

I run the PowerShell command to get total RAM capacity using C# and it returns this value. @{TotalPhysicalMemory= 17856325}. I wrote a code to get the only integer value. This is the code.

string ramSize = "@{TotalPhysicalMemory= 17856325}";
string ramValue = ramSize.Split('=')[1].Split('}')[0].Trim().ToString();

This code returns the integer value. But I want to know are there any ways to get this integer value easily?


Solution

a simple regex to extract the digits in the string will do it.

var ram = new Regex(@"\d+").Match(ramSize).Value;

you can also add System.Management and directly get the size by

            var ram =
            new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT TotalPhysicalMemory FROM Win32_ComputerSystem")
                .Get().OfType<ManagementObject>().First()
                .Properties.OfType<PropertyData>().First().Value;


Answered By - Keith Nicholas
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing