static string SentimentAnalysisExample(TextAnalyticsClient client, string text) { string inputText = text; string response = ""; DocumentSentiment documentSentiment = client.AnalyzeSentiment(inputText); Console.WriteLine($"Document sentiment: {documentSentiment.Sentiment}\n"); response = response + $"Document sentiment: {documentSentiment.Sentiment}\n"; var si = new StringInfo(inputText); foreach (var sentence in documentSentiment.Sentences) { Console.WriteLine($" Text: \"{si.SubstringByTextElements(sentence.GraphemeOffset, sentence.GraphemeLength)}\""); response = response + $" Text: \"{si.SubstringByTextElements(sentence.GraphemeOffset, sentence.GraphemeLength)}\"\n"; Console.WriteLine($" Sentence sentiment: {sentence.Sentiment}"); response = response + $" Sentence sentiment: {sentence.Sentiment}" + "\n"; Console.WriteLine($" Positive score: {sentence.ConfidenceScores.Positive:0.00}"); response = response + $" Positive score: {sentence.ConfidenceScores.Positive:0.00} "; Console.WriteLine($" Negative score: {sentence.ConfidenceScores.Negative:0.00}"); response = response + $" Negative score: {sentence.ConfidenceScores.Negative:0.00} "; Console.WriteLine($" Neutral score: {sentence.ConfidenceScores.Neutral:0.00}\n"); response = response + $" Neutral score: {sentence.ConfidenceScores.Neutral:0.00}\n\n"; } return(response); }
public void SubstringTest(string source, int index, string expected, int length, string expectedWithLength) { StringInfo si = new StringInfo(source); Assert.Equal(expected, si.SubstringByTextElements(index)); Assert.Equal(expectedWithLength, si.SubstringByTextElements(index, length)); }
/// <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)); }
private string GetFixedWidthField(StringInfo Line, Tuple <int, int> FieldLength) { string text; if (FieldLength.Item2 > 0) { text = Line.SubstringByTextElements(FieldLength.Item1 - 1, FieldLength.Item2); } else { if (FieldLength.Item1 - 1 >= Line.LengthInTextElements) { text = string.Empty; } else { text = Line.SubstringByTextElements(FieldLength.Item1 - 1).TrimEnd(new char[] { '\r', '\n' }); } } if (this.m_TrimWhiteSpace) { return(text.Trim()); } return(text); }
public static StringInfo RemoveByTextElements(this StringInfo str, int offset, int count) { return(new StringInfo(string.Concat( str.SubstringByTextElements(0, offset), offset + count < str.LengthInTextElements ? str.SubstringByTextElements(offset + count, str.LengthInTextElements - count - offset) : "" ))); }
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"); }
public static StringInfo InsertByTextElements(this StringInfo str, int offset, string insertStr) { if (string.IsNullOrEmpty(str?.String)) { return(new StringInfo(insertStr)); } return(new StringInfo(string.Concat( str.SubstringByTextElements(0, offset), insertStr, str.LengthInTextElements - offset > 0 ? str.SubstringByTextElements(offset, str.LengthInTextElements - offset) : "" ))); }
public static string Truncate(this string value, int maxLength, bool noPoints = false) { if (string.IsNullOrEmpty(value)) { return(value); } StringInfo strInfo = new StringInfo(value); if (maxLength <= 3 || noPoints) { return(strInfo.LengthInTextElements <= maxLength ? strInfo.String : strInfo.SubstringByTextElements(0, maxLength)); } return(strInfo.LengthInTextElements <= maxLength ? strInfo.String : strInfo.SubstringByTextElements(0, maxLength - 3) + "..."); }
public static StringInfo RemoveByTextElements(this StringInfo str, int offset, int count) { //Tue 20-Aug-19 11:32am metadataconsulting.ca - replaceat index > string.len return orginal string if (offset > str.LengthInTextElements) { return(str); } return(new StringInfo(string.Concat( str.SubstringByTextElements(0, offset), offset + count < str.LengthInTextElements ? str.SubstringByTextElements(offset + count, str.LengthInTextElements - count - offset) : string.Empty ))); }
public void NegativeTest() { string s = "Some String"; StringInfo si = new StringInfo(s); StringInfo siEmpty = new StringInfo(""); Assert.Throws<ArgumentOutOfRangeException>(() => si.SubstringByTextElements(-1)); Assert.Throws<ArgumentOutOfRangeException>(() => si.SubstringByTextElements(s.Length + 1)); Assert.Throws<ArgumentOutOfRangeException>(() => siEmpty.SubstringByTextElements(0)); Assert.Throws<ArgumentOutOfRangeException>(() => si.SubstringByTextElements(-1, 1)); Assert.Throws<ArgumentOutOfRangeException>(() => si.SubstringByTextElements(s.Length + 1, 1)); Assert.Throws<ArgumentOutOfRangeException>(() => siEmpty.SubstringByTextElements(0, 0)); Assert.Throws<ArgumentOutOfRangeException>(() => si.SubstringByTextElements(0, s.Length + 1)); }
public void NegativeTest() { string s = "Some String"; StringInfo si = new StringInfo(s); StringInfo siEmpty = new StringInfo(""); Assert.Throws <ArgumentOutOfRangeException>(() => si.SubstringByTextElements(-1)); Assert.Throws <ArgumentOutOfRangeException>(() => si.SubstringByTextElements(s.Length + 1)); Assert.Throws <ArgumentOutOfRangeException>(() => siEmpty.SubstringByTextElements(0)); Assert.Throws <ArgumentOutOfRangeException>(() => si.SubstringByTextElements(-1, 1)); Assert.Throws <ArgumentOutOfRangeException>(() => si.SubstringByTextElements(s.Length + 1, 1)); Assert.Throws <ArgumentOutOfRangeException>(() => siEmpty.SubstringByTextElements(0, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => si.SubstringByTextElements(0, s.Length + 1)); }
public static string TruncateUnicodeStringByLength(this string value, int maxLength) { if (value == null) { throw new ArgumentNullException("value"); } if (maxLength < 0) { throw new ArgumentOutOfRangeException(string.Format("maxLength must be positive. value: {0} maxLength: {1}", value, maxLength)); } if (value.Length <= maxLength) { return(value); } var textElements = new StringInfo(value); if (textElements.LengthInTextElements <= maxLength) { return(value); } return(textElements.SubstringByTextElements(0, maxLength)); }
public override TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) { var stringInfo = new StringInfo(value: newValue.text); if (this.maxLength != null && this.maxLength > 0 && stringInfo.LengthInTextElements > this.maxLength) { if (Input.compositionString.Length > 0) { return(newValue); } var truncated = stringInfo.SubstringByTextElements(0, lengthInTextElements: this.maxLength.Value); TextSelection newSelection = newValue.selection.copyWith( baseOffset: Mathf.Min(a: newValue.selection.start, b: truncated.Length), extentOffset: Mathf.Min(a: newValue.selection.end, b: truncated.Length) ); return(new TextEditingValue( text: truncated, selection: newSelection, composing: TextRange.empty )); } return(newValue); }
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)); }
public static string ShuffleByTextElements(this string source, Random random) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (random == null) { throw new ArgumentNullException(nameof(random)); } var info = new StringInfo(source); var indices = Enumerable.Range(0, info.LengthInTextElements).ToArray(); // Fisher-Yates shuffle for (var i = indices.Length; i-- > 1;) { var j = random.Next(i + 1); if (i != j) { var temp = indices[i]; indices[i] = indices[j]; indices[j] = temp; } } var builder = new StringBuilder(source.Length); foreach (var index in indices) { builder.Append(info.SubstringByTextElements(index, 1)); } return(builder.ToString()); }
public void Erase(int from, int to, Color?background = null) { // due to TrimEnd() (near the end of this method) the number of sublines can go down - but this in fact // cannot happen during erase operation, so we make sure that the minimal subline count is the current count minimalSublineCount = SublineCount; var builder = new StringBuilder(); var stringInfo = new StringInfo(content); from = Math.Max(0, Math.Min(from, stringInfo.LengthInTextElements)); if (from > 0) { builder.Append(stringInfo.SubstringByTextElements(0, from)); } if (to > from) { builder.Append(' ', to - from); } if (to < stringInfo.LengthInTextElements) { builder.Append(stringInfo.SubstringByTextElements(to)); } for (var i = from; i <= to; i++) { if (background.HasValue) { CheckDictionary(ref specialBackgrounds); specialBackgrounds[i] = background.Value; } else { if (specialBackgrounds != null) { specialBackgrounds.Remove(i); } } if (specialForegrounds != null) { specialForegrounds.Remove(i); } } content = builder.ToString().TrimEnd().PadRight(from, ' '); lengthInTextElements = new StringInfo(content).LengthInTextElements; }
public static string Ellipsize(string str, int elementCount) { var si = new StringInfo(str); var ellipsis = (elementCount < si.LengthInTextElements) ? "..." : string.Empty; var count = Math.Min(si.LengthInTextElements, elementCount); return(si.SubstringByTextElements(0, count) + ellipsis); }
/// <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()); }
/// <summary> /// Split a string into same length of text elements<br/> /// https://codereview.stackexchange.com/a/112018 /// </summary> public static string[] SplitIntoFixedLength(this string value, int length, bool strict = false) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (value.Length == 0 && length != 0) { throw new ArgumentException($"The passed {nameof(value)} may not be empty if the {nameof(length)} != 0"); } StringInfo stringInfo = new StringInfo(value); int valueLength = stringInfo.LengthInTextElements; if (valueLength != 0 && length < 1) { throw new ArgumentException($"The value of {nameof(length)} needs to be > 0"); } if (strict && valueLength % length != 0) { throw new ArgumentException($"The passed {nameof(value)}'s length can't be split by the {nameof(length)}"); } int currentLength = stringInfo.LengthInTextElements; if (currentLength == 0) { return(new string[0]); } int numberOfItems = currentLength / length; int remaining = currentLength > numberOfItems * length ? 1 : 0; string[] chunks = new string[numberOfItems + remaining]; for (int i = 0; i < numberOfItems; i++) { chunks[i] = stringInfo.SubstringByTextElements(i * length, length); } if (remaining != 0) { chunks[numberOfItems] = stringInfo.SubstringByTextElements(numberOfItems * length); } return(chunks); }
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); }
private void button2_Click(object sender, EventArgs e) { int code = 0; string str1 = richTextBox1.Text; string str2 = ""; str1 = str1.Replace("\n", ""); str1 = str1.Replace("\u200b", ""); StringInfo ss = new StringInfo(str1); int m = ss.LengthInTextElements;//字符长度 string word = ""; string strKey = ""; for (int i = 0; i < m; i++) { word = ss.SubstringByTextElements(i, 1); if (word == "\\") { if (strKey != "") { try { code = Convert.ToInt32(strKey, 16); } catch (FormatException) { MessageBox.Show("有非法字符!"); } str2 = str2 + Char.ConvertFromUtf32(code); } strKey = ""; } else { strKey += word; } } if (strKey != "") { try { code = Convert.ToInt32(strKey, 16); } catch (FormatException) { MessageBox.Show("有非法字符!"); } str2 = str2 + Char.ConvertFromUtf32(code); } richTextBox2.Text = str2; }
public static string SubstringWithSC(this string str, int startIndex, int length) { if (str.IsNullOrEmpty()) { return(String.Empty); } StringInfo strInfo = new StringInfo(str); return(strInfo.SubstringByTextElements(startIndex, length)); }
/// <summary> /// 获取指定长度的字符串 /// </summary> /// <param name="content"></param> /// <param name="startIndex"></param> /// <param name="length"></param> /// <returns></returns> public static string Substring(string content, int startIndex, int length) { var value = new StringInfo(content); if (value.LengthInTextElements > length) { return(value.SubstringByTextElements(startIndex, length) + "..."); } return(content); }
public static StringInfo InsertByTextElements(this StringInfo str, int offset, string insertStr) { //Tue 20-Aug-19 11:32am metadataconsulting.ca - replaceat index > string.len return orginal string if (offset > str.LengthInTextElements) { return(str); } if (string.IsNullOrEmpty(str.String)) { return(new StringInfo(insertStr)); } return(new StringInfo(string.Concat( str.SubstringByTextElements(0, offset), insertStr, str.LengthInTextElements - offset > 0 ? str.SubstringByTextElements(offset, str.LengthInTextElements - offset) : "" ))); }
private static void SubstringByTextElements(String s) { String output = String.Empty; StringInfo si = new StringInfo(s); for (Int32 element = 0; element < si.LengthInTextElements; element++) { output += String.Format("Text element {0} is '{1}'{2}", element, si.SubstringByTextElements(element, 1), Environment.NewLine); } Console.WriteLine(output); }
private static void SubstringByTextElements(string s) { string output = string.Empty; StringInfo si = new StringInfo(s); for (int i = 0; i < si.LengthInTextElements; i++) { output += string.Format("Text element {0} is '{1}' {2}", i, si.SubstringByTextElements(i, 1), Environment.NewLine); } MessageBox.Show(output, "Result of ParceCombiningCharacters"); }
static void SubstringByTextElements(string s) { string output = string.Empty; StringInfo si = new StringInfo(s); for (int element = 0; element < si.LengthInTextElements; element++) { output += string.Format("Text element{0} is '{1}' {2}", element, si.SubstringByTextElements(element, 1), Environment.NewLine); } MessageBox.Show(output, "Result of SubstringByTextElements"); }
/// <summary> /// 按文本字符截取字符串,通常用于包含emoji的字符串 /// </summary> /// <param name="input"></param> /// <param name="start"></param> /// <param name="length"></param> /// <returns></returns> public static string SubStringByTextElements(this string input, int start, int length) { var stringInfo = new StringInfo(input); if (start == 0 && stringInfo.LengthInTextElements <= length) { return(input); } return(stringInfo.SubstringByTextElements(start, length)); }
/// <summary> /// 依據傳入長度分割字串 /// </summary> /// <param name="str"></param> /// <param name="chunkSize"></param> /// <returns></returns> public static IEnumerable <string> SplitWithLength(string str, int desiredLength, bool strict = false) { if (desiredLength < 1) { throw new Exception("傳入的長度參數小於1 :[" + desiredLength + "]"); } if (str == null || str == "") { return(new List <string>()); } EnsureValid(str, desiredLength, strict); var stringInfo = new StringInfo(str); int currentLength = stringInfo.LengthInTextElements; if (currentLength == 0) { return(new string[0]); } int numberOfItems = currentLength / desiredLength; int remaining = (currentLength > numberOfItems * desiredLength) ? 1 : 0; var chunks = new string[numberOfItems + remaining]; for (var i = 0; i < numberOfItems; i++) { chunks[i] = stringInfo.SubstringByTextElements(i * desiredLength, desiredLength); } if (remaining != 0) { chunks[numberOfItems] = stringInfo.SubstringByTextElements(numberOfItems * desiredLength); } return(chunks); }
public char this[int i] { get { return(stringInfo.SubstringByTextElements(i, 1)[0]); } }
private static void SubstringByTextElements(string s) { var output = string.Empty; var stringInfo = new StringInfo(s); for (var element = 0; element < stringInfo.LengthInTextElements; element++) { output += string.Format("Text element {0} is '{1}'{2}", element, stringInfo.SubstringByTextElements(element, 1), Environment.NewLine); } Console.WriteLine(output); }
private static void SubstringByTextElements(String s) { String output = String.Empty; StringInfo si = new StringInfo(s); for (Int32 element = 0; element < si.LengthInTextElements; element++) { output += String.Format( "Text element {0} is '{1}'{2}", element, si.SubstringByTextElements(element, 1), Environment.NewLine); } MessageBox.Show(output, "Result of SubstringByTextElements"); }