?
Solved

Find or REPLACE

Posted on 2004-09-24
4
Medium Priority
?
194 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

830 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