views:

36

answers:

3

I have a string

it contains can contains either of (1)

 strQuery = "TempTable.Group_No IN ()";

(2)

strQuery = "TempTable.Group_No IN (1,2,3,4,....)";

My task is if it contains "TempTable.Group_No IN ()", (i.e) IN CLAUSE without data

i have to replace the string (IN() with IN (NULL)) with "TempTable.Group_No IN (NULL)"

How to perform it in C#?

+2  A: 

How about...

strQuery = strQuery.Replace("()", "(NULL)");

... or is that a bit too simple?

James Gaunt
A: 

I don't know how much you have to worry about the empty parenthesis in other areas, but you can use this to have greater certainty that you're replacing the right thing.

strQuery = strQuery.Replace("IN ()", "IN (NULL)");
palswim
+1  A: 

I'm assuming the input is an array of integers for the solution below. Try this out as a console app:

static void Main(string[] args)
{
    int[] ids1 = new int[] { 1, 2, 3, 4, 5 };
    int[] ids2 = new int[] {};

    Console.WriteLine(FormatQuery(ids1));
    Console.WriteLine(FormatQuery(ids2));           
}

static string FormatQuery(int[] ids)
{
    return string.Format("TempTable.Group_No IN ({0})", 
        ids.Length > 0 ? string.Join(",", ids) : "NULL");
}
Adam