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

44 views Asked by At

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..

2

There are 2 answers

0
I_Am_K On BEST ANSWER

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

3
firatt_ On
public static class StringHelper
{
    public static string Formatter(this string value,bool toUpper, string original ,string toReplace,int startIndex , int endIndex)
    {
       return value.ToUpper().Replace(original ,toReplace).Substring(startIndex,endIndex);     
    }
    public static string Formatter(this string value,bool toUpper, string original ,string toReplace)
    {
       return value.ToUpper().Replace(original ,toReplace);     
    }
    public static string Formatter(this string value,bool toUpper,int startIndex , int endIndex)
    {
       return value.ToUpper().Substring(startIndex,endIndex);     
    }
    public static string Formatter(this string value,int startIndex , int endIndex)
    {
       return value.Substring(startIndex,endIndex);     
    }
    public static string Formatter(this string value, string original ,string toReplace)
    {
       return value.Replace(original ,toReplace);     
    }
}

    Console.WriteLine("test".Formatter(true,"T","TS",0,3)); 
    Console.WriteLine("test".Formatter(true,"T","TS"));
    Console.WriteLine("test".Formatter(true,"T","TS"));
    Console.WriteLine("test".Formatter(0,4));
    Console.WriteLine("test".Formatter("T","TS"));