简体   繁体   中英

How to get numbers from string in C#?

string str="this image has the width of 200px and height of 100px.";
int width, height;

How can I code to get

width= 200;
height=100;

If width is more than 150, i will reduce the width to 150 and i will calculate the height

if(width>150)
{
 height=(height*150)/200;
 width=150;
}

And the result string would be..

str="this image has the width of 150px and height of 75px.";

I know substring() indexOf() split()

I know i can split the string with "width" but I dunno how to find the number of that splitted string.

You can do it like this:

string str = "this image has the width of 200px and height of 100px.";
string[] numbers = Regex.Split(str, @"\D+");
// let's say 100x100 is default, so if TryParse fails you get 100x100
int width = 100;
int height = 100;
if (numbers.Length > 1)
{
    Int32.TryParse(numbers[0], out width);
    Int32.TryParse(numbers[1], out height);
}

You can do a regular expression match and save 200 + 100.

Dim match As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(str, "this image has the width of (\d+)px and height of (\d+)px.")

Afterwards, look the values up in:

match.Groups(0).Value
match.Groups(1).Value

For C# it's

match.Groups[0].Value;
match.Groups[1].Value;

int.Parse (string) or int.TryParse (string, out int) are probably the methods you want to use to convert a string to an int.

For example:

int width;
if (int.TryParse (widthString, out width) == true)
{
    Console.WriteLine ("The width is {0}", width);
}
else
{
    Console.WriteLine ("{0} could not be converted to an int.", widthString);
}

If the format of the string is always like that it becomes a rather trivial task. You basically just have to split the string with " " as the separator, and get the strings from the resulting array at position 6 and 10. Then just subtract the last 2 characters "px" and TryParse to int. You have it. Or just use some fancy Regex, like others will surely suggest.

There's a few parts to this. First I would suggest a regular expression to match part of the string and explicitly capture the numeric parts of it.

eg, a possible regular expression could be:

width\sof\s(?<width>\d+)px\sand\sheight\sof\s(?<height>\d+)px

Which matches "width of XXXpx and height of YYYpx" where XXX is captured as a named group width and YYY is captured as a named group height .

Here's the code so far:

string str="this image has the width of 200px and height of 100px.";
var regex = new Regex(@"width\sof\s(?<width>\d+)px\sand\sheight\sof\s(?<height>\d+)px");
var match = regex.Match(str);

Now that match has 2 groups named width and height as previously described. Then you use can use int.Parse on the capture groups to get the values as numbers:

var width = int.Parse(match.Groups["width"].Value);
var height = int.Parse(match.Groups["height"].Value);

The last part, changing the number and re-formulating the string you seem to have a good grasp of, so I havent included that.

Here's a live example to demonstrate all of the above: http://rextester.com/rundotnet?code=CLPS3633

Use Regex,Matches method

 string str = "this image has the width of 200px and height of 100px.";
 var  numbers = Regex.Matches(str,@"\d+");
 int no1, no2;

 if (numbers.Count == 2)
  {
    int.TryParse(numbers[0].Value, out no1);
    int.TryParse(numbers[1].Value, out no2);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM