Solved

Find or REPLACE

Posted on 2004-09-24
4
186 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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