1

I have a string and need to replace some content based on certain substrings appearing in the string. e.g. a sample string might be

(it.FirstField = "fred" AND it.SecondField = True AND it.ThirdField = False AND it.FifthField = True)

and I want to transform it to:

(it.FirstField = "fred" AND it.SecondField = 'Y' AND it.ThirdField = 'N' AND it.FifthField = True)

i.e. if the substring appears in the string, I want to change the True to 'Y' and the False to 'N', but leave any other True/False values intact.

I have an array of substrings to look for:

 string[] booleanFields = { "SecondField", "ThirdField", "FourthField" };

I can use something like if (booleanFields.Any(s => inputString.Contains(s))) to find out if the string contains any of the keywords, but what's the best way to perform the replacement?

Thanks.

Justin
  • 84,773
  • 49
  • 224
  • 367
  • I think you need to provide a more general description of the problem. Your problem description is so closely tied to your perceived implementation (which is confusing as you are trying to change the type of fields from bool to char) that we might do better with a higher-level problem description. – BJ Safdie May 04 '11 at 00:45
  • Here goes with a more general description - I have an application that is building a query to be passed to a web service which in turn queries a database. However the application sees some fields as being boolean fields, but in the database they are 'Y' or 'N' string values. I don't have the ability to change either the application or the database, so need something that can convert the query coming from the application into terms the database can deal with. – Neil Mercer May 04 '11 at 01:51

2 Answers2

1

In the words of clipit - it looks like you are trying to parse SQL, would you like some help with that?

You can try and do this via string manipulation, but you are going to run into problems - think about what would happen if you replaced "fred" with something else, perhaps:

(it.FirstField = "it.SecondField = True" AND it.SecondField = True)

I'm loathed to recommend it (because it's probably quite difficult), but the correct way to do this is to parse the SQL and manipulate the parsed expression - see Parsing SQL code in C# for what looks like an approach that could make this relatively straightfoward.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
0

It's probably not the best answer due to the two very similar lines (one for true/one for false), but this works and is fairly neat for a Regex (with .Dump() ready for LINQPad paste).

It does however assume that you want to replace every ".FieldName = True" within your content (which will include cases where this format is enclosed in quotes as a string value).

void Main()
{
    List<string> booleanFields = new List<string> { "SecondField", "ThirdField", "FourthField" };
    string s = @"(it.FirstField = ""fred"" AND it.SecondField = True AND it.ThirdField = False AND it.FifthField = True)";

    booleanFields.ForEach(bf => s = Regex.Replace(s, String.Format(@"[.]{0}[ ]*=[ ]*True", bf), String.Format(".{0} = 'Y'", bf)));
    booleanFields.ForEach(bf => s = Regex.Replace(s, String.Format(@"[.]{0}[ ]*=[ ]*False", bf), String.Format(".{0} = 'N'", bf)));

    s.Dump();
}
Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185