Exemplo n.º 1
0
        /// <summary>
        ///  Transliterate a string based on a given transliteration table
        /// </summary>
        /// <param name="sValue"></param>
        /// <param name="transliterationTable"></param>
        /// <returns></returns>
        public static string Transliterate(this string sValue, TransliterationTable transliterationTable)
        {
            sValue.EnsureHasValue();
            if (transliterationTable?.Table == null)
            {
                throw new ArgumentNullException(nameof(transliterationTable));
            }

            return(TransliterateFromTable(sValue, transliterationTable));
        }
Exemplo n.º 2
0
        private static string TransliterateFromTable(string sValue, TransliterationTable tranTable)
        {
            var sb = new StringBuilder();

            foreach (char c in sValue)
            {
                if (char.IsDigit(c))
                {
                    sb.Append(c.ToString());
                }
                else
                {
                    sb.Append(tranTable.Table.TryGetValue(c.ToString(), out string translited) ? translited : "?");
                }
            }

            return(sb.ToString());
        }