Need help/advise in C# string operation

mohantyd
mohantyd used Ask the Experts™
on
Need help in code for string operation in following type condition.

Original string = " XXXXXXX  #A# XXXXXX #B# XXXXXXX #N# .........

#A#,#B# ... are excel sheet column names and i dont know upto what number those column name will go and may not come sequentially. So one option is read it from start and getting first # read till next # and take inbetween data and replace it with value like Column(A), Column(B) etc.

Is there any better way to do this ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hi,

You could use regex like this

      string input = " XXXXXXX  #A# XXXXXX #B# XXXXXXX #N# .........";
      string pattern = @"([^#]*?)#([^#]+?)#";

      MatchCollection ms = Regex.Matches(input, pattern);
      foreach (Match m in ms) {
        Console.WriteLine(string.Format("Column({0}) = '{1}'", m.Groups[2].Value, m.Groups[2].Value));
      }

/peter

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial