Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET
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
Comments
Wes Aday at 28-Aug-14 11:20am
   
Okay.... what is it that you need help with?
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
v2
Comments
rosoftghana at 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 at 29-Aug-14 9:19am
   
Solution updated.
Yvan Rodrigues at 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 at 29-Aug-14 9:25am
   
My 5.
Rate this: bad
good
Please Sign up or sign in to vote.

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);
            }
  Permalink  
Comments
rosoftghana at 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 at 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: bad
good
Please Sign up or sign in to vote.

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
}
  Permalink  
Comments
rosoftghana at 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 at 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)

  Print Answers RSS
0 OriginalGriff 384
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 CPallini 170
4 Shweta N Mishra 165
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 29 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100