Creates a string based on the arguments If no args are applied, then we want to just return the message - CSharp System

CSharp examples for System:String Format

Description

Creates a string based on the arguments If no args are applied, then we want to just return the message

Demo Code

//  Copyright 2018 Magenic, All rights Reserved
using System.Text;
using System;/*  ww  w.  j a  va2s.  co m*/

public class Main{
        /// <summary>
        /// Creates a string based on the arguments
        /// If no args are applied, then we want to just return the message
        /// </summary>
        /// <param name="message">The message being used</param>
        /// <param name="args">The arguments being used</param>
        /// <returns>A final string</returns>
        /// <example>
        /// <code source = "../UtilitiesUnitTests/StringProcessorUnitTests.cs" region="StringFormattor" lang="C#" />
        /// </example>
        public static string SafeFormatter(string message, params object[] args)
        {
            try
            {
                return string.Format(message, args);
            }
            catch (Exception)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(message);
                foreach (var arg in args)
                {
                    builder.AppendLine(arg.ToString());
                }

                return builder.ToString();
            }
        }
}

Related Tutorials