System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list


        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder(); 
            string test = "124454664{0}89jnhb";
            string formattedMessage = string.Format(test, args);
   
        }

主要原因是字符串中有{}, 被string.Format調用時報錯。

Replace即可。

            StringBuilder sb = new StringBuilder(); 
            string test = "124454664{0}89jnhb";
            test = test.Replace("{", "[").Replace("}", "]"); 
            string formattedMessage = string.Format(test, args);
   

或者 將 { 替換為 {{


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM