/// <summary> /// From a given name, increment it /// myFileAsset -> myFileAsset 1 /// myFileAsset 58 -> myFileAsset 59 /// </summary> /// <param name="nameToIncrement">name to increment</param> /// <param name="index">new index incremented</param> /// <returns>new incremented name</returns> public static string IncrementName(string nameToIncrement, out int index) { index = ExtString.ExtractIntFromEndOfString(nameToIncrement); string nameWithoutNumber = nameToIncrement.Replace(index.ToString(), ""); nameWithoutNumber = nameWithoutNumber.TrimEnd(' '); index += 1; return(nameWithoutNumber + " " + index); }
/// <summary> /// from a given string, like: RaceTrack 102 /// extract the number 102 (int) /// </summary> /// <param name="input"></param> /// <returns>number in the end of string</returns> public static int ExtractIntFromEndOfString(string input) { var stack = new Stack <char>(); for (var i = input.Length - 1; i >= 0; i--) { if (!char.IsNumber(input[i])) { break; } stack.Push(input[i]); } string result = new string(stack.ToArray()); return(ExtString.ToInt(result)); }