Beispiel #1
0
        public static string Format(string formatString, object obj)
        {
            var sb = new StringBuilder();

            bool inBrace = false;
            var token = new StringBuilder();

            var reader = new StringReader(formatString);
            int ich;
            while ((ich = reader.Read()) >= 0) {
                char ch = (char)ich;
                int inext = reader.Peek();
                char next = inext >= 0 ? (char)inext : (char)0;

                switch (ch) {
                    case '{':
                        if (next == '{') {
                            reader.Read();
                            sb.Append(ch);
                        } else {
                            token.Clear();
                            inBrace = true;
                        }
                        break;
                    case '}':
                        if (next == '}') {
                            reader.Read();
                            sb.Append(ch);
                        } else {

                            inBrace = false;
                            string separator = "";
                            if (next == '[') {
                                reader.Read(); // read the [
                                separator = reader.ReadUntilNext(']');
                            }
                            sb.Append(SubstituteToken(token.ToString(), obj, separator));
                        }
                        break;
                    default:
                        if (inBrace) {
                            token.Append(ch);
                        } else {
                            sb.Append(ch);
                        }
                        break;
                }
            }

            return sb.ToString();
        }