12,077,753 members (48,879 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%.

Please I need help.
Posted 28-Aug-14 6:04am
Wes Aday 28-Aug-14 11:20am

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

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.
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("'"))
{
// do your thing
}
```
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.

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

Top Experts
Last 24hrsThis month
 Richard MacCutchan 365 Richard Deeming 305 Sergey Alexandrovich Kryukov 243 CHill60 200 OriginalGriff 195
 Dave Kreskowiak 3,026 OriginalGriff 3,000 Richard MacCutchan 2,539 CPallini 1,722 CHill60 1,579

Advertise | Privacy | Mobile
Web01 | 2.8.160212.1 | Last Updated 29 Aug 2014