コード例 #1
0
        public static string NormalizeWhitespace(this string text, char whitespaceChar, char[] whitespace)
        {
            if (text == null)
            {
                string message = "Can't process null string";
                throw new ArgumentNullException("text", message);
            }
            StringBuilder stringBuilder = new StringBuilder(text.Length);
            bool          flag          = false;

            char[] array = text.ToCharArray();
            for (int i = 0; i < array.Length; i++)
            {
                char c = array[i];
                if (LatinWordExtensions.IsDelimiter(c, whitespace))
                {
                    if (!flag)
                    {
                        stringBuilder.Append(whitespaceChar);
                    }
                    flag = true;
                }
                else
                {
                    stringBuilder.Append(c);
                    flag = false;
                }
            }
            return(stringBuilder.ToString());
        }
コード例 #2
0
        public static string Wrap(this string text, int lineLength, string newLineMarker)
        {
            if (text == null)
            {
                string message = "Can't process null string";
                throw new ArgumentNullException("text", message);
            }
            if (lineLength <= 0)
            {
                return(text);
            }
            if (newLineMarker == null)
            {
                newLineMarker = Environment.NewLine;
            }
            int           num           = 0;
            int           capacity      = (int)Math.Round(text.Length * 1.2m);
            StringBuilder stringBuilder = new StringBuilder(capacity);
            int           length        = text.Length;

            for (int i = 0; i < length; i++)
            {
                char c       = text[i];
                int  length2 = stringBuilder.Length;
                if (LatinWordExtensions.IsDelimiter(c, LatinWordExtensions.WhiteSpaceCharacters))
                {
                    num = length2;
                }
                stringBuilder.Append(c);
                if (length2 > 0 && length2 % lineLength == 0)
                {
                    stringBuilder.Remove(num, 1);
                    stringBuilder.Insert(num, newLineMarker);
                }
            }
            if (text.Length % lineLength > 0)
            {
                stringBuilder.Remove(num, 1);
                stringBuilder.Insert(num, newLineMarker);
            }
            return(stringBuilder.ToString());
        }