13,146,014 members (43,849 online)
Rate this:
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%.

Posted 28-Aug-14 5:04am

Okay.... what is it that you need help with?

Rate this:

## Solution 2

Off the top of my head:

```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.

v2
rosoftghana 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.
Rate this:

## Solution 3

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.
```Regex regex = new Regex(@"\d+(?=')");
string replaced = regex.Replace(inputString, new MatchEvaluator(ReplaceMatch));
```
The ReplaceMatch method would be along the lines of:
```private string ReplaceMatch(Match m)
{
double feet = double.Parse(m.Value);
return string.Format("{0:N2}", feet * 0.3048);
}
```
rosoftghana 29-Aug-14 5:21am

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.
Rate this:

## Solution 1

You can split them using the Split() method on the string. Split on ' and then loop through the results.

Something like:

```foreach (String str in stringVar.Split("'"))
{
}```
rosoftghana 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
RyanDev 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.

Top Experts
Last 24hrsThis month
 OriginalGriff 405 CPallini 210 ppolymorphe 110 Jochen Arndt 90 Dave Kreskowiak 60
 OriginalGriff 5,925 Graeme_Grant 5,051 ppolymorphe 1,984 Jochen Arndt 1,904 CPallini 1,805