Looking for C# equivalent of scanf

后端 未结 9 1149
误落风尘
误落风尘 2020-11-29 09:51

I used to code in C language in the past and I found the scanf function very useful. Unfortunately, there is no equivalent in C#.

I am using using it to

9条回答
  •  孤街浪徒
    2020-11-29 10:45

    I have found a better solution than using sscanf from C or some rewritten part by someone (no offence)

    http://msdn.microsoft.com/en-us/library/63ew9az0.aspx have a look at this article, it explains how to make named groups to extract the wanted data from a patterned string. Beware of the little error in the article and the better version below. (the colon was not part of the group)

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          string url = "http://www.contoso.com:8080/letters/readme.html";
          Regex r = new Regex(@"^(?\w+)://[^/]+?(?:\d+)?/",RegexOptions.None, TimeSpan.FromMilliseconds(150));
          Match m = r.Match(url);
          if (m.Success)
             Console.WriteLine(r.Match(url).Result("${proto}:${port}")); 
       }
    }
    // The example displays the following output: 
    //       http::8080
    

提交回复
热议问题