Looking for C# equivalent of scanf

后端 未结 9 1141
误落风尘
误落风尘 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

    Although this looks sort of crude it does get the job done:

    // Create the stream reader
    sr = new StreamReader("myFile.txt");
    
    // Read it
    srRec = sr.ReadLine();
    
    // Replace multiple spaces with one space
    String rec1 = srRec.Replace("          ", " ");
    String rec2 = rec1.Replace("         ", " ");
    String rec3 = rec2.Replace("        ", " ");
    String rec4 = rec3.Replace("       ", " ");
    String rec5 = rec4.Replace("      ", " ");
    String rec6 = rec5.Replace("     ", " ");
    String rec7 = rec6.Replace("    ", " ");
    String rec8 = rec7.Replace("   ", " ");
    String rec9 = rec8.Replace("  ", " ");
    
    // Finally split the string into an array of strings with Split
    String[] srVals = rec9.Split(' ');
    

    You can then use the array srVals as individual variables from the record.

提交回复
热议问题