Пример #1
0
        /// <summary>Retrieves a substring of text elements from the current <see cref="T:System.Globalization.StringInfo" /> object starting from a specified text element and continuing through the specified number of text elements.</summary>
        /// <returns>A substring of text elements in this <see cref="T:System.Globalization.StringInfo" /> object. The substring consists of the number of text elements specified by the <paramref name="lengthInTextElements" /> parameter and starts from the text element index specified by the <paramref name="startingTextElement" /> parameter.</returns>
        /// <param name="startingTextElement">The zero-based index of a text element in this <see cref="T:System.Globalization.StringInfo" /> object.</param>
        /// <param name="lengthInTextElements">The number of text elements to retrieve.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///   <paramref name="startingTextElement" /> is less than zero.-or-<paramref name="startingTextElement" /> is greater than or equal to the length of the string that is the value of the current <see cref="T:System.Globalization.StringInfo" /> object.-or-<paramref name="lengthInTextElements" /> is less than zero.-or-The string that is the value of the current <see cref="T:System.Globalization.StringInfo" /> object is the empty string ("").-or-<paramref name="startingTextElement" /> + <paramref name="lengthInTextElements" /> specify an index that is greater than the number of text elements in this <see cref="T:System.Globalization.StringInfo" /> object.</exception>
        public string SubstringByTextElements(int startingTextElement, int lengthInTextElements)
        {
            if (startingTextElement < 0 || this.s.Length == 0)
            {
                throw new ArgumentOutOfRangeException("startingTextElement");
            }
            if (lengthInTextElements < 0)
            {
                throw new ArgumentOutOfRangeException("lengthInTextElements");
            }
            int num = 0;

            for (int i = 0; i < startingTextElement; i++)
            {
                if (num >= this.s.Length)
                {
                    throw new ArgumentOutOfRangeException("startingTextElement");
                }
                num += StringInfo.GetNextTextElementLength(this.s, num);
            }
            int num2 = num;

            for (int j = 0; j < lengthInTextElements; j++)
            {
                if (num >= this.s.Length)
                {
                    throw new ArgumentOutOfRangeException("lengthInTextElements");
                }
                num += StringInfo.GetNextTextElementLength(this.s, num);
            }
            return(this.s.Substring(num2, num - num2));
        }
Пример #2
0
        /// <summary>Retrieves a substring of text elements from the current <see cref="T:System.Globalization.StringInfo" /> object starting from a specified text element and continuing through the last text element.</summary>
        /// <returns>A substring of text elements in this <see cref="T:System.Globalization.StringInfo" /> object, starting from the text element index specified by the <paramref name="startingTextElement" /> parameter and continuing through the last text element in this object.</returns>
        /// <param name="startingTextElement">The zero-based index of a text element in this <see cref="T:System.Globalization.StringInfo" /> object.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///   <paramref name="startingTextElement" /> is less than zero.-or-The string that is the value of the current <see cref="T:System.Globalization.StringInfo" /> object is the empty string ("").</exception>
        public string SubstringByTextElements(int startingTextElement)
        {
            if (startingTextElement < 0 || this.s.Length == 0)
            {
                throw new ArgumentOutOfRangeException("startingTextElement");
            }
            int num = 0;

            for (int i = 0; i < startingTextElement; i++)
            {
                if (num >= this.s.Length)
                {
                    throw new ArgumentOutOfRangeException("startingTextElement");
                }
                num += StringInfo.GetNextTextElementLength(this.s, num);
            }
            return(this.s.Substring(num));
        }
Пример #3
0
        /// <summary>Gets the text element at the specified index of the specified string.</summary>
        /// <returns>A string containing the text element at the specified index of the specified string.</returns>
        /// <param name="str">The string from which to get the text element. </param>
        /// <param name="index">The zero-based index at which the text element starts. </param>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="str" /> is null. </exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///   <paramref name="index" /> is outside the range of valid indexes for <paramref name="str" />. </exception>
        public static string GetNextTextElement(string str, int index)
        {
            int nextTextElementLength = StringInfo.GetNextTextElementLength(str, index);

            return((nextTextElementLength == 1) ? new string(str[index], 1) : str.Substring(index, nextTextElementLength));
        }