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

后端 未结 8 1391
故里飘歌
故里飘歌 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

    @mquander: Actualy, PHP solves it even different:

    $s = "This is very very funny.";
    $fmt = "This %s very %s.";
    sscanf($s, $fmt, $one, $two);
    echo "
    one: [$one], two: [$two]
    \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).

提交回复
热议问题