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.
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:
Which matches “width of XXXpx and height of YYYpx” where XXX is captured as a named group
widthand YYY is captured as a named groupheight.Here’s the code so far:
Now that
matchhas 2 groups namedwidthandheightas previously described. Then you use can useint.Parseon the capture groups to get the values as numbers: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