本文整理汇总了C#中System.Globalization.StringInfo.SubstringByTextElements方法的典型用法代码示例。如果您正苦于以下问题:C# StringInfo.SubstringByTextElements方法的具体用法?C# StringInfo.SubstringByTextElements怎么用?C# StringInfo.SubstringByTextElements使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Globalization.StringInfo
的用法示例。
在下文中一共展示了StringInfo.SubstringByTextElements方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SubstringByTextElements
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: CapitalizeFirstLetter
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: Truncate
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;
}
}
示例4: ConvertToCharIndex
/// <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;
}
示例5: UpdateStatus
/// <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;
}
}
}
示例6: GetPropertyDisplayName
/// <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();
}
示例7: ConvertToCharIndex
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;
}
示例8: FormatPropertyName
/// <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);
}
示例9: PeekChars
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);
}
示例10: GetFixedWidthField
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;
}
示例11: EditValue
/// <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;
}
示例12: ToPascalCase
/// <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();
}