class Program
    {
        static void Main(string[] args)
        {
            string[] lines = System.IO.File.ReadAllLines(@"d:\score.txt",Encoding.Default);
            score.txt内容:
            赵子龙|18|500
            关羽|19|300
            典韦|20|200
            string maxName = "";//定义maxName记录成绩最高者姓名
            int maxScore = -1;//定义maxScore记录最高者成绩
            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];
                string[] strs = line.Split('|');
                Console.WriteLine("{0}", strs);
             /*输出:
            赵子龙
            关羽
            典韦
            不用占位符(貌似是这么读)"{0}"时,输出:
            System.String[]
            System.String[]
            System.String[]
            难道是带了"{0}"就输出字符串,那数字不会当成字符串输出吗?求解!
            */
             
             }
            Console.ReadKey();
        }
    }             static void Main(string[] args)
        {
            string lines = "赵子龙|18|500";
            string[] str = lines.Split('|');
             for (int i = 0; i < lines.Length; i++)
            {
               string line =[u] lines[i][/u];
               string[] strs = lines.Split('|');
            }
         }
         这段没读取文本,本身不是字符串类型吗?为什么提示无法将char类型转换成string?

解决方案 »

  1.   

    lines[i],这是lines字符串中的第i个字符,是char类型
      

  2.   

    lines是string
    但lines[i]是char类型
      

  3.   

    可强制转换到string类型就行了!
      

  4.   

    那第二个lines[i]和第一个lines[i]有什么区别,第一个不报错!
      

  5.   

    第一个的lines是string[]字符数组,所以lines[i]是第i个元素,lines[i]是字符串。
    第二个的lines是string字符串,所以lines[i]是第i个字符,lines[i]是字符。
      

  6.   

    问题一,{0},是数字时候
    public static string Format(
    string format,
    Object[] args
    )上面是MSDN上定义的Format语法,说明Object[] args是Oject类型,所以数字没问题
      

  7.   

    using System;
    using System.Globalization;class Sample 
    {
        enum Color {Yellow = 1, Blue, Green};
        static DateTime thisDate = DateTime.Now;    public static void Main() 
        {
    // Store the output of the String.Format method in a string.
        string s = "";    Console.Clear();// Format a negative integer or floating-point number in various ways.
        Console.WriteLine("Standard Numeric Format Specifiers");
        s = String.Format(CultureInfo.InvariantCulture, 
            "(C) Currency: . . . . . . . . {0:C}\n" +
            "(D) Decimal:. . . . . . . . . {0:D}\n" +
            "(E) Scientific: . . . . . . . {1:E}\n" +
            "(F) Fixed point:. . . . . . . {1:F}\n" +
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(N) Number: . . . . . . . . . {0:N}\n" +
            "(P) Percent:. . . . . . . . . {1:P}\n" +
            "(R) Round-trip: . . . . . . . {1:R}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n",
            -123, -123.45f); 
        Console.WriteLine(s);// Format the current date in various ways.
        Console.WriteLine("Standard DateTime Format Specifiers");
        s = String.Format(CultureInfo.InvariantCulture.DateTimeFormat, 
            "(d) Short date: . . . . . . . {0:d}\n" +
            "(D) Long date:. . . . . . . . {0:D}\n" +
            "(t) Short time: . . . . . . . {0:t}\n" +
            "(T) Long time:. . . . . . . . {0:T}\n" +
            "(f) Full date/short time: . . {0:f}\n" +
            "(F) Full date/long time:. . . {0:F}\n" +
            "(g) General date/short time:. {0:g}\n" +
            "(G) General date/long time: . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(M) Month:. . . . . . . . . . {0:M}\n" +
            "(R) RFC1123:. . . . . . . . . {0:R}\n" +
            "(s) Sortable: . . . . . . . . {0:s}\n" +
            "(u) Universal sortable: . . . {0:u} (invariant)\n" +
            "(U) Universal sortable: . . . {0:U}\n" +
            "(Y) Year: . . . . . . . . . . {0:Y}\n", 
            thisDate);
        Console.WriteLine(s);// Format a Color enumeration value in various ways.
        Console.WriteLine("Standard Enumeration Format Specifiers");
        s = String.Format(CultureInfo.InvariantCulture, 
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
            "(D) Decimal number: . . . . . {0:D}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n", 
            Color.Green);       
        Console.WriteLine(s);
        }
    }
      

  8.   

    你的输出是一个对象,所以输出是System.String[]
    如果要达到目的,换成:Console.WriteLine("{0}", strs[0]);
      

  9.   

    还是不懂为什么不输出数字和为什么输出System.String[]!
      

  10.   

    因为Console.WriteLine("{0}", strs);
    中的strs是个System.String[]对象,所以结果肯定输出对象名。
    如果要你想要的效果,改成
    Console.WriteLine("{0}", strs[0]);