是否要计算不包含RTF格式的字符串中的字符? (C#)

时间:2019-01-08 22:32:42

标签: c# string unity3d character richtext

平台:Unity 2018.2.20f1 语言:C#.Net 4.x

场景

我有一个字符串,该字符串将应用于Unity中渲染的textmesh。此渲染管道支持RTF文本编辑。 http://digitalnativestudios.com/textmeshpro/docs/rich-text/

请求

因此,当我提供字符串时,我想知道字符计数,该计数将排除用于RTF格式的字符。

示例

string _value = "We are <b><i>definitely not</i></b> amused";
// Character count should be 29 instead of 43

那么实现它的最佳方法是什么?是否有在线模块/资源可以帮助我提取计数?

谢谢你, 卡斯嫩

2 个答案:

答案 0 :(得分:1)

您可以设置如何替换格式块。

string CountNumber = Regex.Replace(richtextbox, "<.*?>", String.Empty);
MessageBox.Show(CountNumber.Length);

答案 1 :(得分:0)

解决方案

面向非社区开发人员

根据我的理解,您将不得不使用Regex表达式来解决它。建议使用@Wubber && @Yakov。 Another answer with suggested by Wubber

对于使用TMPro.TextMeshProUGUI的Unity开发人员

TMPro.TMP_InputField myInputField;
TMPro.TextMeshProUGUI InputTextBoxField;

private void onValueChangedInMyInputField(string _value)
{
  int _charCount = InputTextBoxField.GetParsedText().Length;
  int _inputFieldCount = myInputField.text.Length;
  // _charCount is the number excluding the rich text content 
  // _inputFieldCount is the number inclusive of the rich text content
}

所有人,谢谢您的时间。干杯。

相关问题