Solved

Find or REPLACE

Posted on 2004-09-24
4
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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