Search code examples
c#asp.netbackendrazorengine

How to invoke multiple function from string "Mystring.ToUpper().FirstPart(3).Replace("M","Ok") C# .net


In server side want do a nested operation with a string. .It will be in dynamic format. I'm trying with razor engine for this. Any ideas.. Eg: "Mystring.ToUpper().FirstPart(3).Replace("M","Ok")" This string mentioned with the functions name.

Var input =  "Mystring.ToUpper().FirstPart(3).Replace("M","Ok");
FirstPart(int val){
.
.
return result;
}

output : OkYS

"Mystring.ToUpper().FirstPart(3).Replace("M","Ok")

Mystring.ToUpper() = MYSTRING,

MYSTRING.FirstPart(3) = MYS,

MYS.Replace("M","Ok")= OkYS

Note: Input will be dynamic.

"Mystring.ToUpper().FirstPart(3).Replace("M","Ok")"

(or)

"Mystring.ToUpper().Replace("M","Ok")"

(or)

"Mystring.ToLower().FirstPart(3)..

or any..


Solution

  • For this case i used dotLiquid

    Ex:

    var template = DotLiquid.Template.Parse("Hello {{ username | upcase | trimval"}}")
    

    My input is "Hello {{ username | upcase | trimval"}}"

    trimval is a function which i called from outside.

    var rendered = template.Render(Hash.FromAnonymousObject(new { username = username}));
    

    When render this i'll get the answer .If my input (user = "stackF").Assume Trimval return first 5 chars.

    Output = STACK