Is there an equivalent to 'sscanf()' in .NET?

后端 未结 8 1393
故里飘歌
故里飘歌 2020-12-10 11:46

The .NET Framework gives us the Format method:

string s = string.Format(\"This {0} very {1}.\", \"is\", \"funny\");
// s is now: \"This is very funny.\"


        
相关标签:
8条回答
  • 2020-12-10 12:33

    Yep. These are called "regular expressions". The one that will do the thing is

    This (?<M0>.+) very (?<M1>.+)\.
    
    0 讨论(0)
  • 2020-12-10 12:33

    @mquander: Actualy, PHP solves it even different:

    $s = "This is very very funny.";
    $fmt = "This %s very %s.";
    sscanf($s, $fmt, $one, $two);
    echo "<div>one: [$one], two: [$two]</div>\n";
    //echo's: "one: [is], two: [very]"
    

    But maybe your regular expression remark can help me. I just need to rewrite "This {0} very {1}." to something like: new Regex(@"^This (.*) very (.*)\.$"). This should be done programmatical, so I can use one format string on the public class interface.

    BTW: I've already have a parser to find the parameters: see the Named Format Redux blog entry by Phil Haack (and yes, I also want named paramters to work both ways).

    0 讨论(0)
提交回复
热议问题