2

I have a string which gives the measurement followed the units in either cm, m or inches.

For example :

The number could be 112cm, 1.12m, 45inches or 45in.

I would like to extract only the number part of the string. Any idea how to use the units as the delimiters to extract the number ?

While I am at it, I would like to ignore the case of the units.

Thanks

King King
  • 61,710
  • 16
  • 105
  • 130
Kiran
  • 8,034
  • 36
  • 110
  • 176

6 Answers6

2

Use String.Split http://msdn.microsoft.com/en-us/library/tabh47cf.aspx

Something like:

var units = new[] {"cm", "inches", "in", "m"};
var splitnumber = mynumberstring.Split(units, StringSplitOptions.RemoveEmptyEntries);
var number = Convert.ToInt32(splitnumber[0]);
Marko Juvančič
  • 5,792
  • 1
  • 25
  • 41
2

You can try:

string numberMatch = Regex.Match(measurement, @"\d+\.?\d*").Value;

EDIT

Furthermore, converting this to a double is trivial:

double result;
if (double.TryParse(number, out result))
{
    // Yeiiii I've got myself a double ...
}
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • his number can also be decimal, this is not enough. – King King Aug 01 '13 at 07:30
  • Ya That's right. That was my query too. It does not handle Double does it ? – Kiran Aug 01 '13 at 07:35
  • @www.sapnaedu.in That was not in your question (maybe you should specify it), nevertheless, you can check my edit and get the idea how to handle the different types. – Dimitar Dimitrov Aug 01 '13 at 07:44
  • Thank you so much, I am sorry I have mentioned in my question 1.12m and extract only the number. Is it misleading ? But the regex pattern `@"^\d+"` ignore decimal right and gives you only the integer part. – Kiran Aug 01 '13 at 07:47
  • @www.sapnaedu.in you're right, my regex skills are a bit rusty, sorry :) I've edited it a bit. Needs testing though. – Dimitar Dimitrov Aug 01 '13 at 07:54
2

Using Regex this can help you out:

(?i)(\d+(?:\.\d+)?)(?=c?m|in(?:ch(?:es)?)?)

Break up:

 (?i)                 = ignores characters case // specify it in C#, live do not have it
 \d+(\.\d+)?          = supports numbers like 2, 2.25 etc
 (?=c?m|in(ch(es)?)?) = positive lookahead, check units after the number if they are 
                        m, cm,in,inch,inches, it allows otherwise it is not.
 ?:                   = specifies that the group will not capture
 ?                    = specifies the preceding character or group is optional

Demo

EDIT

Sample code:

MatchCollection mcol = Regex.Matches(sampleStr,@"(?i)(\d+(?:\.\d+)?)(?=c?m|in(?:ch(?:es)?)?)")

foreach(Match m in mcol)
{
    Debug.Print(m.ToString());   // see output window
}
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • 1
    I like this approach the best because it can be applied against strings which may contain other unrelated text. – Ro Yo Mi Aug 01 '13 at 13:57
1

I guess I'd try to replace with "" every character that is not number or ".":

//s is the string you need to convert
string tmp=s;
foreach (char c in s.ToCharArray())
            {
                if (!(c >= '0' && c <= '9') && !(c =='.'))
                    tmp = tmp.Replace(c.ToString(), "");
            }
s=tmp;
Dog
  • 474
  • 8
  • 25
1

Try using regular expression \d+ to find an integer number.

resultString = Regex.Match(measurementunit , @"\d+").Value;
DP.
  • 51
  • 5
  • 1
    This is an answer which i would highly recommend as its the easiest to understand and the cleanest – aceminer Aug 01 '13 at 08:53
0

Is it a requirement that you use the unit as the delimiter? If not, you could extract the number using regex (see Find and extract a number from a string).

Community
  • 1
  • 1
KTHL
  • 119
  • 8
  • No, That was not a requirement. I thought that was the effective way of doing it. I will check your link. Thanks – Kiran Aug 01 '13 at 07:28