0

I am currently using string replace in some parts of the code type= type.Replace("TRIM", "RTRIM");

 type = type.Replace("TRIM", "RTRIM"); 
    StringBuilder builder =newStringBuilder();
    builder.AppendLine(" .....bla bla..."); 
    if (type.Trim() != ""){
    builder.AppendLine(@"   WHERE ({0}) ");
    }

The database current {0} values are

  1. TRIM(VL9) LIKE '{1}%'
  2. TRIM(VL7) LIKE '{0}%' 3.(TRIM(VL9) LIKE '{0}%'))
  3. (VL9 LIKE '{1}%' AND (TRIM(VL9) LIKE '{0}%') )

This is a huge database, and the replace function is used in many files at many places. In future they will change TRIM's to RTRIM's in the database so then in the code(.cs files) replace string becomes RRTRIM which gives us an error.

Is there any simple way to code which works now and after future changes?

PS: originally the .cs queries were in DB2 which are now changed to SQL SERVER queries and now they will be making changes in db like changing TRIM to RTRIM)

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
  • Regex can be used, but for a simple spot replace it probably is overkill. –  Apr 14 '16 at 21:05
  • let says if the the database value is RTRIM VL9 LIKE '{1}%' AND (TRIM(VL9) LIKE '{0}%') ).. this will again make RTRIM and RRTRIM.. so what can be done..?? I am trying to use REGEX..Regex.Replace(type, "(?i)[^A-Za-z]Trim", "RTRIM"); and the output is RTRIM VL9 LIKE '{1}%' AND RTRIM(VL9) LIKE '{0}%') ) one of the braces is missing while replacing how can that be ignored. How can i use regex here? – virtualreality Apr 15 '16 at 18:55

2 Answers2

0

Below code will only run if there is no "RTRIM" in type:

    if (!type.contains("RTRIM"))
        {
            type = type.Replace("TRIM", "RTRIM"); 
            if (!type.contains("RRTRIM"))
            {
                type = type.Replace("RRTRIM", "RTRIM");
            }
            StringBuilder builder =newStringBuilder();
            builder.AppendLine(" .....bla bla..."); 
            if (type.Trim() != ""){
            builder.AppendLine(@"   WHERE ({0}) ");
            }
        }

but since you probably have places with rrtrim already, and maybe even rrrtrim you can do this:

type = type.Replace("TRIM", "RTRIM"); 
type = type.Replace("RRTRIM", "RTRIM");
type = type.Replace("RRRTRIM", "RTRIM");
StringBuilder builder =newStringBuilder();
builder.AppendLine(" .....bla bla..."); 
if (type.Trim() != ""){
builder.AppendLine(@"   WHERE ({0}) ");
Claudius
  • 1,883
  • 2
  • 21
  • 34
  • let says if the the database value is RTRIM VL9 LIKE '{1}%' AND (TRIM(VL9) LIKE '{0}%') ).. this will again make RTRIM and RRTRIM.. so what can be done..?? I am trying to use REGEX..Regex.Replace(type, "(?i)[^A-Za-z]Trim", "RTRIM"); and the output is RTRIM VL9 LIKE '{1}%' AND RTRIM(VL9) LIKE '{0}%') ) one of the braces is missing while replacing how can that be ignored. How can i use regex here? – virtualreality Apr 15 '16 at 18:56
  • see update. That will update all of them then fix whatever was wrong. I'm guessing you are trying out code and some stuff is already changed and some not, so that should work. – Claudius Apr 15 '16 at 19:02
  • it is working but if you see RTRIM VL9 LIKE '{1}%' AND RTRIM(VL9) LIKE '{0}%') ) code which is part of a query might throw error as one of the "(" is missing which "(TRIM" is replaced by "RTRIM".. how can i let just "TRIM" be replaced by"RTRIM" AND letting the "( " character be the way it is. – virtualreality Apr 15 '16 at 19:20
  • above code is not trimming any parenthesis. i think there something wrong with your regex code – Claudius Apr 15 '16 at 19:27
  • [A-Za-z]Trim this instead of this (?i)[^A-Za-z]Trim – Claudius Apr 15 '16 at 19:32
0

Use regex. How to search and replace exact matching strings only

Another simple hack can be first replace all RTRIMs with TRIM first and then replace TRIM with RTRIM

type.Replace("RTRIM","TRIM").Replace("TRIM","RTRIM")
Community
  • 1
  • 1
zealoustechie
  • 83
  • 1
  • 7