SubstringByTextElements() public method

public SubstringByTextElements ( int startingTextElement ) : String
startingTextElement int
return String
コード例 #1
0
		public void SubstringByTextElements ()
		{
			StringInfo si = new StringInfo ("A\u0330BC\u0330");
			Assert.AreEqual ("A\u0330BC\u0330", si.SubstringByTextElements (0), "#1");
			Assert.AreEqual ("BC\u0330", si.SubstringByTextElements (1), "#2");
			Assert.AreEqual ("C\u0330", si.SubstringByTextElements (2), "#3");
		}
コード例 #2
0
 public static string CapitalizeFirstLetter(this string s, CultureInfo ci = null)
 {
     var si = new StringInfo(s);
     if (ci == null)
         ci = CultureInfo.CurrentCulture;
     if (si.LengthInTextElements > 0)
         s = si.SubstringByTextElements(0, 1).ToUpper(ci);
     if (si.LengthInTextElements > 1)
         s += si.SubstringByTextElements(1);
     return s;
 }
コード例 #3
0
        private List <string> CheckPinyin2(string words, List <string> strList2)
        {
            var w   = new System.Globalization.StringInfo(words);
            var len = w.LengthInTextElements;

            if (len != strList2.Count)
            {
                return(null);
            }

            List <string> list = new List <string>();

            for (int i = 0; i < strList2.Count; i++)
            {
                var py    = strList2[i];
                var rtome = RemoveTone(py);
                var t     = w.SubstringByTextElements(i, 1);
                if (noneTomeDict.TryGetValue(t, out List <string> pys))
                {
                    if (pys.Contains(rtome) == false)
                    {
                        return(null);
                    }
                }
            }

            foreach (var item in strList2)
            {
                var py = CheckPinyin(item);
                list.Add(py);
            }
            return(list);
        }
コード例 #4
0
    /// <summary>Returns a string array that contains the substrings in this string that are seperated a given fixed length.</summary>
    /// <param name="s">This string object.</param>
    /// <param name="length">Size of each substring.
    /// <para>CASE: length &gt; 0 , RESULT: String is split from left to right.</para>
    /// <para>CASE: length == 0 , RESULT: String is returned as the only entry in the array.</para>
    /// <para>CASE: length &lt; 0 , RESULT: String is split from right to left.</para>
    /// </param>
    /// <returns>String array that has been split into substrings of equal length.</returns>
    /// <example>
    /// <code>
    /// string s = "1234567890";
    /// string[] a = s.Split(4); // a == { "1234", "5678", "90" }
    /// </code>
    /// </example>
    public static string[] Split(this string s, int length)
    {
        System.Globalization.StringInfo str = new System.Globalization.StringInfo(s);

        int lengthAbs = Math.Abs(length);

        if (str.LengthInTextElements == 0 || lengthAbs == 0 || str.LengthInTextElements <= lengthAbs)
        {
            return(new string[] { str.String });
        }

        string[] array =
            new string[
                (str.LengthInTextElements % lengthAbs == 0
                     ? str.LengthInTextElements / lengthAbs
                     : (str.LengthInTextElements / lengthAbs) + 1)];

        if (length > 0)
        {
            for (int iStr = 0, iArray = 0; iStr < str.LengthInTextElements && iArray < array.Length; iStr += lengthAbs, iArray++)
            {
                array[iArray] = str.SubstringByTextElements(
                    iStr, (str.LengthInTextElements - iStr < lengthAbs ? str.LengthInTextElements - iStr : lengthAbs));
            }
        }

        else
        {
            for (int iStr = str.LengthInTextElements - 1, iArray = array.Length - 1;
                 iStr >= 0 && iArray >= 0;
                 iStr -= lengthAbs, iArray--)
            {
                array[iArray] = str.SubstringByTextElements(
                    (iStr - lengthAbs < 0 ? 0 : iStr - lengthAbs + 1), (iStr - lengthAbs < 0 ? iStr + 1 : lengthAbs));
            }
        }

        return(array);
    }
コード例 #5
0
ファイル: HtmlHelpers.cs プロジェクト: wulab/prototype.net
        public static string Truncate(this HtmlHelper helper, string input, int length, string omission)
        {
            // http://dobon.net/vb/dotnet/string/substring.html
            
            StringInfo si = new StringInfo(input);

            if (si.LengthInTextElements <= length)
            {
                return input;
            }
            else
            {
                return si.SubstringByTextElements(0, length) + omission;
            }
        }
コード例 #6
0
ファイル: LinkLabel.cs プロジェクト: JianwenSun/cc
        /// <devdoc>
        ///     Converts the character index into char index of the string
        ///     This method is copied in LinkCollectionEditor.cs. Update the other
        ///     one as well if you change this method.
        ///     This method mainly deal with surrogate. Suppose we 
        ///     have a string consisting of 3 surrogates, and we want the
        ///     second character, then the index we need should be 2 instead of
        ///     1, and this method returns the correct index.
        /// </devdoc>
        private static int ConvertToCharIndex(int index, string text) {
            if (index <= 0) {
                return 0;
            }
            if (String.IsNullOrEmpty(text)) {
                Debug.Assert(text != null, "string should not be null"); 
                //do no conversion, just return the original value passed in
                return index;
            }

            //VSWhidbey 217272: Dealing with surrogate characters
            //in some languages, characters can expand over multiple
            //chars, using StringInfo lets us properly deal with it.
            StringInfo stringInfo = new StringInfo(text);
            int numTextElements = stringInfo.LengthInTextElements;

            //index is greater than the length of the string
            if (index > numTextElements) {
                return index - numTextElements + text.Length;  //pretend all the characters after are ASCII characters
            }
            //return the length of the substring which has specified number of characters
            string sub = stringInfo.SubstringByTextElements(0, index);
            return sub.Length;
        }
コード例 #7
0
        /// <summary>
        /// ステータス更新を行う
        /// </summary>
        /// <param name="text"></param>
        private void UpdateStatus(string text)
        {
            // 140字以下かどうかのチェックを行い、超えている場合はconfigに従って動作する
            StringInfo stringInfo = new StringInfo(text);
            int tweetLength = stringInfo.LengthInTextElements;

            if (tweetLength < 140)
            {
                oauth.UpdateStatus(text, this.inReplyToStatusId);
            }
            else
            {
                string subText = "";
                switch (config.TreatTooLongTweetAs)
                {
                    case 1:
                        subText = stringInfo.SubstringByTextElements(0, 137);
                        subText += "...";

                        TweetTextBox.Enabled = false;
                        oauth.UpdateStatus(subText, this.inReplyToStatusId);
                        break;
                    case 2:
                        subText = stringInfo.SubstringByTextElements(0, 133);
                        subText += "[...続く]";
                        oauth.UpdateStatus(subText, this.inReplyToStatusId);

                        UpdateStatus(stringInfo.SubstringByTextElements(133));
                        break;
                    default:
                        KumaHodaiToolStripStatusLabel.Text = "エラー: 文字数が140文字を超えています。";
                        break;
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Gets display name of a property.
        /// For example:
        /// ID => ID
        /// EmployeeName => Employee Name
        /// EmployeeXMLName => Employee XML Name
        /// EmployeeXML => Employee XML
        /// Employee_XML => Employee XML
        /// </summary>
        /// <param name="propertyName">The property name.</param>
        /// <returns>Display name</returns>
        public static string GetPropertyDisplayName(string propertyName)
        {
            Debug.Assert(!string.IsNullOrEmpty(propertyName));

            if (string.IsNullOrEmpty(propertyName)) return "";

            StringInfo propertyNameInfo = new StringInfo(propertyName);
            StringBuilder nameBuilder = new StringBuilder();
            string currentUpperCaseSection = "";
            for (int i = 0; i < propertyNameInfo.LengthInTextElements; i++)
            {
                bool hasContentBefore = nameBuilder.Length > 0;
                string current = propertyNameInfo.SubstringByTextElements(i, 1);
                bool currentIsUpper = Char.IsUpper(current, 0);
                if (currentIsUpper)
                {
                    if (hasContentBefore && currentUpperCaseSection.Length == 0)
                    {
                        currentUpperCaseSection = " ";
                    }
                    currentUpperCaseSection = currentUpperCaseSection + current;
                    continue;
                }
                else if (currentUpperCaseSection.Length > 0)
                {
                    currentUpperCaseSection = currentUpperCaseSection.TrimEnd();
                    if (currentUpperCaseSection.Length > 2)
                    {
                        nameBuilder.Append(currentUpperCaseSection.Substring(0, currentUpperCaseSection.Length - 1));
                        nameBuilder.Append(" ");
                        currentUpperCaseSection = currentUpperCaseSection.Substring(currentUpperCaseSection.Length - 1);
                    }
                    nameBuilder.Append(currentUpperCaseSection);
                    currentUpperCaseSection = "";
                }

                if (string.Equals(current, "_", StringComparison.OrdinalIgnoreCase))
                {
                    // Convert "_" as " "
                    if (hasContentBefore && currentUpperCaseSection.Length == 0)
                    {
                        currentUpperCaseSection = " ";
                    }
                    continue;
                }
                
                nameBuilder.Append(current);
            }

            if (currentUpperCaseSection.Length > 0)
            {
                nameBuilder.Append(currentUpperCaseSection.TrimEnd());
            }

            return nameBuilder.ToString();
        }
コード例 #9
0
 private static int ConvertToCharIndex(int index, string text)
 {
     if (index <= 0)
     {
         return 0;
     }
     if (string.IsNullOrEmpty(text))
     {
         return index;
     }
     StringInfo info = new StringInfo(text);
     int lengthInTextElements = info.LengthInTextElements;
     if (index > lengthInTextElements)
     {
         return ((index - lengthInTextElements) + text.Length);
     }
     return info.SubstringByTextElements(0, index).Length;
 }
コード例 #10
0
 /// <summary>
 /// Formats the name of the property.
 /// </summary>
 /// <param name="variableName">Name of the variable.</param>
 /// <returns></returns>
 private static string FormatPropertyName(string variableName)
 {
     StringInfo si = new StringInfo(variableName);
     return si.SubstringByTextElements(0, 1).ToUpperInvariant() +
            si.SubstringByTextElements(1, si.LengthInTextElements - 1);
 }
コード例 #11
0
 public string PeekChars(int numberOfChars)
 {
     if (numberOfChars <= 0)
     {
         throw ExceptionUtils.GetArgumentExceptionWithArgName("numberOfChars", "TextFieldParser_NumberOfCharsMustBePositive", new string[] { "numberOfChars" });
     }
     if ((this.m_Reader == null) | (this.m_Buffer == null))
     {
         return null;
     }
     if (this.m_EndOfData)
     {
         return null;
     }
     string str = this.PeekNextDataLine();
     if (str == null)
     {
         this.m_EndOfData = true;
         return null;
     }
     str = str.TrimEnd(new char[] { '\r', '\n' });
     if (str.Length < numberOfChars)
     {
         return str;
     }
     StringInfo info = new StringInfo(str);
     return info.SubstringByTextElements(0, numberOfChars);
 }
コード例 #12
0
 private string GetFixedWidthField(StringInfo Line, int Index, int FieldLength)
 {
     string str;
     if (FieldLength > 0)
     {
         str = Line.SubstringByTextElements(Index, FieldLength);
     }
     else if (Index >= Line.LengthInTextElements)
     {
         str = string.Empty;
     }
     else
     {
         str = Line.SubstringByTextElements(Index).TrimEnd(new char[] { '\r', '\n' });
     }
     if (this.m_TrimWhiteSpace)
     {
         return str.Trim();
     }
     return str;
 }
コード例 #13
0
        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            this.provider = provider;

            if(provider != null)
            {
                IWindowsFormsEditorService service =
                    (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if(service == null)
                {
                    return value;
                }

                this.openFileDialog = new OpenFileDialog();
                this.openFileDialog.Multiselect = true;
                this.InitializeDialog(this.openFileDialog);

                if(value is string)
                {
                    String filename = (String)value;
                    if (!filename.Contains("\""))
                    {
                        this.openFileDialog.FileName = filename;
                    }
                    else
                    {
                        this.openFileDialog.FileName = String.Empty;
                    }
                }

                if(this.openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    if (this.openFileDialog.FileNames != null && this.openFileDialog.FileNames.Length > 1)
                    {
                        String files = String.Empty;
                        foreach (String str in this.openFileDialog.FileNames)
                        {
                            files = files + "\"" + str + "\" ";
                        }
                        StringInfo si = new StringInfo(files);
                        value = si.SubstringByTextElements(0, si.LengthInTextElements - 1);
                    }
                    else
                    {
                        value = this.openFileDialog.FileName;
                    }
                }
            }
            return value;
        }
コード例 #14
0
        /// <summary>
        /// Converts a string of dash-separated, or underscore-separated words to a PascalCase string.
        /// </summary>
        /// <param name="s">The string to convert.</param>
        /// <returns>The resulting PascalCase string.</returns>
        public static string ToPascalCase(this string s)
        {
            var words = s.Split(new char[3] { '-', '_', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            
            var sb = new ExtendedStringBuilder(words.Sum(x => x.Length));

            foreach (string word in words)
            {
                var stringInfo = new StringInfo(word);
                sb += stringInfo.SubstringByTextElements(0, 1).ToUpper();
                sb += stringInfo.SubstringByTextElements(1).ToLower();
            }

            return sb.ToString();
        }