Link to home
Start Free TrialLog in
Avatar of fpoyavo
fpoyavoFlag for United States of America

asked on

Find or REPLACE

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.

Avatar of tomasX2
tomasX2

           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);
            }            
        }


ASKER CERTIFIED SOLUTION
Avatar of tzxie2000
tzxie2000
Flag of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fpoyavo

ASKER

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.
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