Solved

Find or REPLACE

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get String split 5 48
Performance  Html.BeginForm vs jQuery 3 49
Linq Help 1 35
Entity Framework 7 32
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

12 Experts available now in Live!

Get 1:1 Help Now