Solved

Find or REPLACE

Posted on 2004-09-24
4
183 Views
Last Modified: 2010-04-15
hI Experts,

How do I find or replace first comma which is not within double quotes in my string ?

Example :

(BBBB , "CCCC, DDDD")

Thank you.

0
Comment
Question by:fpoyavo
  • 2
4 Comments
 
LVL 5

Expert Comment

by:tomasX2
ID: 12148698
           string stringToProcess = "(\"BBBB ,\" ,\"CCCC, DDDD\" \"\" \",\" ,\"\"  ) ";
            CheckString(stringToProcess);

        private void CheckString(string s)
        {
            System.Diagnostics.Debug.WriteLine(s);
            int index = IndexOfFirstCommaNotWithinDoubleQuote(s, s,0);
            System.Diagnostics.Debug.WriteLine(index);
        }
        private static int IndexOfFirstCommaNotWithinDoubleQuote(string original, string temp, int index)
        {
            int indexOfComma        = temp.IndexOf(',');
            int indexOfFirstQuote   = temp.IndexOf('"');
            int indexOfSecondQuote  = temp.IndexOf('"', indexOfFirstQuote+1);

            if (indexOfComma < indexOfFirstQuote)
            {
                return index + indexOfComma;
            }
            else
            {
                index = index + indexOfSecondQuote+1;
                return IndexOfFirstCommaNotWithinDoubleQuote(original, temp.Substring(indexOfSecondQuote+1), index);
            }            
        }


0
 
LVL 5

Accepted Solution

by:
tzxie2000 earned 500 total points
ID: 12148944
private void button3_Click(object sender, System.EventArgs e)
    {
      string s="BBBB , \"CCCC, DDDD\"";
      int index=SearchComma(s);
      s="\"BBBB , \"\"CCCC\", DDDD\"";
      index=SearchComma(s);
    }

private int SearchComma(string s)
    {
      int commaIndex,quateIndex,resIndex;
      bool inQuate;

      resIndex=0;
      inQuate=false;
      for(;;)
      {
        commaIndex=s.IndexOf(",",resIndex);
        quateIndex=s.IndexOf("\"",resIndex);
        if(quateIndex<0)
        {
          if((!inQuate)&&(commaIndex>=0))
          {
            resIndex=commaIndex;
            break;
          }
          resIndex=-1;
          break;
        }
        if(commaIndex<0)
        {
          resIndex=-1;
          break;
        }
        if(quateIndex<commaIndex)
        {
          if(!inQuate)
          {
            resIndex=commaIndex+1;
            inQuate=true;
            continue;
          }
          resIndex=commaIndex;
          break;
        }
        if(inQuate)
        {
          resIndex=quateIndex+1;
          inQuate=false;
          continue;
        }
        resIndex=commaIndex;
        break;
      }
      return resIndex;
    }
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 12150251
Hi,

Example does not mean (BBBB , "CCCC, DDDD") that BBBB CCCC DDDD the only ones. There may be any number of characters...
any characters.

Few more examples :

(any number of characters and/or spaces , "any number of characters and/or spaces , any number of characters and/or spaces , any number of characters and/or spaces etc")

Thanks.
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12150691
yes the code could do for and string
you can check it will what you want

the function will return -1 when you enter error string like ("abas,)
other times it will work well
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now