Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string FormatString(string format, params object[] vars)
- {
- List<int>[] lists = new List<int>[vars.Length];
- int lastIndex = 0;
- for (int i = 0; i < vars.Length; i++)
- {
- lists[i] = new List<int>();
- int index = -1;
- string current = "{" + i + "}";
- do
- {
- index = format.ctIndexOf(current, lastIndex + 1);
- if (index != -1)
- lists[i].Add(index);
- lastIndex = index;
- } while (index != -1);
- lastIndex = 0;
- }
- for (int i = lists.Length - 1; i >= 0; i--)
- {
- string current = "{" + i + "}";
- for (int j = lists[i].Count - 1; i >= 0; i--)
- {
- int removeAt = lists[i][j];
- format = format.Remove(removeAt, current.Length);
- format = format.Insert(removeAt, vars[i].ToString());
- }
- }
- return format;
- }
- public static int ctIndexOf(this string str, string find, int startIndex = 0)
- {
- for (int i = startIndex; i < (str.Length - find.Length) + 1; i++)
- {
- bool isfound = true;
- for (int j = 0; j < find.Length; j++)
- {
- if (str[i + j] != find[j])
- {
- isfound = false;
- break;
- }
- }
- if (isfound)
- return i;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment