简体   繁体   English

如何从C#中的字符串中获取数字?

[英]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 如果宽度超过150,我将宽度减小到150,我将计算高度

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() 我知道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. 您可以进行正则表达式匹配并保存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 对于C#来说

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. int.Parse(string)或int.TryParse(string,out int)可能是您要用于将字符串转换为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. 你基本上只需要用“”分隔字符串作为分隔符,并从位置6和10的结果数组中获取字符串。然后只需将最后2个字符“px”和TryParse减去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 . 其中“XXXpx的宽度和YYYpx的高度”匹配,其中XXX被捕获为命名组width ,YYY被捕获为命名组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. 现在,该match有2个组,如前所述,名称为widthheight Then you use can use int.Parse on the capture groups to get the values as numbers: 然后使用可以在捕获组上使用int.Parse来获取值作为数字:

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 以下是演示以上所有内容的实例: 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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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