locked
Parse string RRS feed

  • Question

  • Hello

    I've got this string "DATA(HEX_ONE= 67#, HEX_TWO= CA#)"

    I want to remove the word DATA the brackets ( and ) and the hashtags #  so I just have "HEX_ONE=67, HEX_TWO=CA"

    Currently I'm just doing a .replace("DATA", "").replace.("(", "").replace(")","").Replace("#", "");

    There has got to be a better more professional way to do that?

    Tuesday, June 10, 2014 8:25 PM

Answers

  • Hi,

    If you really want "HEX_ONE=67, HEX_TWO=CA" as your output, and the input format is fixed, continue with the code you have. It might seem a bit clunky now, but its very readable, and in 6 months you (and your colleagues) will still understand exactly what it does. Readibility of the mark of a professional, in my book.

    If you want to do something more (for example extracting the HEX_ONE and HEX_TWO values), then surely look at regular expressions. Be warned though, that specific regular expressions are famously hard to read, and you'll probably be wondering how it works next week, never mind in 6 months!

    This regular expression will extract the "67" and "CA" (or whatever values they have), into groups called hexOne and hexTwo;

    "^DATA\(HEX_ONE=\s*(?<hexOne>\w*)#,\s*HEX_TWO=\s*(?<hexTwo>\w*)#\)"

    Powerful, but really at the expense of readability.

    Regards,

    Nick.

    Wednesday, June 11, 2014 10:02 AM

All replies

  • One of the most flexible ways of searching and extracting substrings is to use a regular expression. Unfortunately, it takes a fair bit of background reading to get an understanding of this technique. In the end, you may find this a technique you will use over and over.

    http://msdn.microsoft.com/en-us/library/hs600312%28v=vs.110%29.aspx

    http://www.dotnetperls.com/regex-match

    Tuesday, June 10, 2014 10:05 PM
  • Not sure if this is any better ...

        static void Main(string[] args)
        {
          string x = "DATA(HEX_ONE= 67#, HEX_TWO= CA#)";
          int i1 = x.IndexOf('(');
          int i2 = x.IndexOf(')');
          
          string y = x.Substring(i1+1,i2-i1-1).Replace("#","");
          Console.WriteLine(y);
        }


    • Edited by conrarn Wednesday, June 11, 2014 3:02 PM
    Tuesday, June 10, 2014 11:27 PM
  • Everything is going to be a variation on the string manipulation you are already doing. As long as it is readable then it should be OK. If it needs to be reusable you could wrap it in an extension method.

    var result = yourString.Split(new char[] { '(', ')' })[1].Replace("#", "");

    Wednesday, June 11, 2014 12:05 AM
  • Hello,

       If you just remove word from string , use String.Replace method. you want take string as formula and process the formula, you need to parse formula and remove formula node.  

    -----------------------------------------
     if the reply help you mark it as your answer.
     Free Managed C# Word PDF , Excel Component(Create, Modify, Convert & Print) 
    Wednesday, June 11, 2014 1:51 AM
  • Is the glass half full or half empty.  You can look at the problem two ways.  Either removing what you don't want, or extract what you only want.  Regex class is one method for manipulating strings.  See code below.

    Use the following page for Regex language

    http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input = "DATA(HEX_ONE= 67#, HEX_TWO= CA#)";
                string pattern = @"\((?'data'[^\)]*)";
                string output = Regex.Match(input, pattern).Groups["data"].Value.Replace("#", "");
            }
        }
    }
    


    jdweng

    Wednesday, June 11, 2014 3:49 AM
  • If the string has a fixed format, then try this variation too:

        string s = "DATA(HEX_ONE= 67#, HEX_TWO= CA#)";

        string r = string.Format( "HEX_ONE={0}{1}, HEX_TWO={2}{3}", s[14], s[15], s[28], s[29] );

    Wednesday, June 11, 2014 6:49 AM
  • Hi,

    If you really want "HEX_ONE=67, HEX_TWO=CA" as your output, and the input format is fixed, continue with the code you have. It might seem a bit clunky now, but its very readable, and in 6 months you (and your colleagues) will still understand exactly what it does. Readibility of the mark of a professional, in my book.

    If you want to do something more (for example extracting the HEX_ONE and HEX_TWO values), then surely look at regular expressions. Be warned though, that specific regular expressions are famously hard to read, and you'll probably be wondering how it works next week, never mind in 6 months!

    This regular expression will extract the "67" and "CA" (or whatever values they have), into groups called hexOne and hexTwo;

    "^DATA\(HEX_ONE=\s*(?<hexOne>\w*)#,\s*HEX_TWO=\s*(?<hexTwo>\w*)#\)"

    Powerful, but really at the expense of readability.

    Regards,

    Nick.

    Wednesday, June 11, 2014 10:02 AM