string.FormatInvariant(object)

Here are the examples of the csharp api string.FormatInvariant(object) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

19 View Source File : StringExtensions.cs
License : Apache License 2.0
Project Creator : microsoft

public static string QuoteArgument(this string arg) {
            if (arg == null) {
                // Null arguments are excluded
                return null;
            }

            if (string.IsNullOrEmpty(arg)) {
                // Empty string means empty argument
                return "\"\"";
            }

            if (arg.Length == 1) {
                // Single character never needs quoting
                return arg;
            }

            if (arg.First() == '"' && arg.Last() == '"') {
                if (!FindUnescapedChar(arg, '"', 1, arg.Length).Any()) {
                    // Already quoted correctly
                    return arg;
                }
                // Needs re-quoting, so strip the existing quotes
                // We do not want to return unquoted though
                arg = arg.Substring(1, arg.Length - 2);
            } else if (arg.IndexOf(' ') < 0 && !FindUnescapedChar(arg, '"').Any()) {
                // Does not need quoting
                return arg;
            }

            if (arg.Length > 1 && arg[arg.Length - 1] == '\\' && arg[arg.Length - 2] != '\\') {
                // Need to escape the trailing backslash
                arg += '\\';
            }

            foreach (var i in FindUnescapedChar(arg, '"').Reverse().ToArray()) {
                // We are going to quote with double quotes, so escape any
                // inline double quotes first
                arg = arg.Insert(i, "\\");
            }

            return "\"{0}\"".FormatInvariant(arg);
        }