Some application can’t work with characters larger then 2 bytes. You can replace these character by a question mark ‘?’ with the following code:

        /// <summary>
        /// Replace all characters with more then 2 bytes by a "?" character
        /// Trim start and end
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string Replace2BytesOrMoreCharactersAndTrim(string input)
        {
            if(string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }

            // Loop characters in input string
            char[] characters = input.ToCharArray();
            StringBuilder resultStrinBuilder = new StringBuilder(string.Empty);
            foreach (char character in characters)
            {
                // Get utf-8 bytes of character
                byte[] characterInBytes = Encoding.UTF8.GetBytes(new char[] { character });

                if (characterInBytes.Length > 2)
                {
                    // Replace character by "?" for character with more then 2 bytes
                    resultStrinBuilder.Append("?");
                }
                else
                {
                    // Keep original character
                    resultStrinBuilder.Append(character);
                }
            }

            // Remove start en trailing white spaces.
            string result = resultStrinBuilder.ToString().Trim();

            // Converting to UTF-8 creates a bom, remove the bom
            result = result.TrimStart('?');

            return result;
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.