Home > Mobile >  How can I get the input to be the same as the output string using my DoStuff Method?
How can I get the input to be the same as the output string using my DoStuff Method?

Time:11-23

My method doesn't deliver the "expect" string that I expect to get from my input string. He should close the parenthesis after the list of words.

 public static string Dostuff(string st)
        {
            String s = "";
        
           String pattern = @"[^($]";
          


            if (st.Contains("create view"))
            {
                s = st.Replace("create view", "CSQL_CREATE_VIEW (");
            }
            if (s.Contains("CSQL_CREATE_VIEW (") /*&& Regex.IsMatch(st,pattern)*/ ) 
            {
                s = s   ")";
            
        
            }
            return s;
        }
  

   static void Main(string[] args)
        {

//Test
input = "create view etwas.viewiges()";
expect = "CSQL_CREATE_VIEW ( etwas.viewiges)()";
output = Dostuff(input);          
         if (expect != output)
         throw new Exception();


CodePudding user response:

expect is CSQL_CREATE_VIEW ( etwas.viewiges)() but

output is CSQL_CREATE_VIEW ( etwas.viewiges())

What is the purpose of pattern? You don't use it anwhere.

CodePudding user response:

Changing DoStuff to the version below will work regardless of the case of the 'create view' part of the source string. It will also preserve any parameters passed.

public static string DoStuff(string source)
{
    // Return the original string if it does not need renaming.
    if (!source.ToLower().StartsWith("create view "))
        return source;

    // Strip off the 'CREATE VIEW ' part. We use string index and range rather
    // than string.Replace so we don't have to worry about source string case.
    source = source[12..];

    // Isolate the view name and any parameters in parentheses.
    string viewName;
    string parameters;
    var openBracketPosition = source.IndexOf('(');

    if (openBracketPosition == -1)
    {
        // There are no parameters in parentheses.
        viewName = source;
        parameters = string.Empty;
    }
    else
    {
        // There are parameters in parentheses.
        viewName = source[..openBracketPosition];
        parameters = source[openBracketPosition..];
    }

    return $"CSQL_CREATE_VIEW ({viewName}){parameters}";
}

Test Input

create view my.view(some stuff)
create view my.view()
create view my.view
CREATE VIEW MY.VIEW(some stuff)

Test Output

CSQL_CREATE_VIEW (my.view)(some stuff)
CSQL_CREATE_VIEW (my.view)()
CSQL_CREATE_VIEW (my.view)
CSQL_CREATE_VIEW (MY.VIEW)(some stuff)
  • Related