Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
2.45/5 (4 votes)
See more:
Hi All

I want to convert figures with ending with "'" to meters in a string.

e.g I have

The core heavily broken from 239'-242': 23'-28' with a core recovery of 25%.

I want this

The core heavily broken from 72.85-73.76: 7.01-8.53 with a core recovery of 25%.

Please I need help.
Posted
Comments
[no name] 28-Aug-14 11:20am    
Okay.... what is it that you need help with?

Off the top of my head:

C#
using System.Text.RegularExpressions;

static string Convert(string value)
{
    return Regex.Replace(value, @"([\d.]+)'", m => (
        float.Parse(m.Groups[1].Value) * 0.3048 +     // Convert feet to metres
        m.Groups[2].Success ? float.Parse(m.Groups[1].Value) * 0.0254 : 0     // Convert inches to metres.
    ).ToString());
}


What is says is:

  • Find all occurrences of a number followed by an apostrophe.
  • Convert the numerical value to metres.
  • Substitute all occurrences.
 
Share this answer
 
v2
Comments
wizy@2020 29-Aug-14 5:20am    
I am very happy with your solution.

But in case I have something like this

QUARTZ with minor schist partings from 7'8''-8'2''

I want
QUARTZ with minor schist partings from 2.34-2.49
Yvan Rodrigues 29-Aug-14 9:19am    
Solution updated.
Yvan Rodrigues 29-Aug-14 9:20am    
PS: Regular Expressions are pretty amazing. Read http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial. It will change your life.
RaisKazi 29-Aug-14 9:25am    
My 5.
First, extract your numbers-ending-in-quote with a regex.
You can then use the Regex.Match overload that takes a MatchEvaluator as teh second parameter to feed each match individually to a function, which returns the new string.
C#
Regex regex = new Regex(@"\d+(?=')");
string replaced = regex.Replace(inputString, new MatchEvaluator(ReplaceMatch));
The ReplaceMatch method would be along the lines of:
C#
private string ReplaceMatch(Match m)
    {
    double feet = double.Parse(m.Value);
    return string.Format("{0:N2}", feet * 0.3048);
    }
 
Share this answer
 
Comments
wizy@2020 29-Aug-14 5:21am    
I like your suggestion.

But in case I have something like this

QUARTZ with minor schist partings from 7'8''-8'2''

I want
QUARTZ with minor schist partings from 2.34-2.49
OriginalGriff 29-Aug-14 5:55am    
So all you have to do is change the regex and the method to cope with it.
Probably, the regex would have two groups (one for feet, one for inches so the feet could be optional) and the method would use the Match.Groups to do one or two conversions depending on the input.
You can split them using the Split() method on the string. Split on ' and then loop through the results.

Something like:

C#
foreach (String str in stringVar.Split("'"))
{
  // do your thing
}
 
Share this answer
 
Comments
wizy@2020 29-Aug-14 5:22am    
What if I have something like this

QUARTZ with minor schist partings from 7'8''-8'2''

I want
QUARTZ with minor schist partings from 2.34-2.49
ZurdoDev 29-Aug-14 7:27am    
You'll have to parse the data. There is no magic to just do what you want. You'll have to finish the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900