【问题标题】:Would like to count the characters in a string excluding rich text formatting? (C#)想要计算字符串中不包括富文本格式的字符? (C#)
【发布时间】:2019-06-03 16:05:17
【问题描述】:

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

场景

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

请求

因此,当我提供字符串时,我想知道字符数,这将排除用于富文本格式的字符。

示例

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

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

谢谢你, 卡斯南

【问题讨论】:

  • 想到用正则表达式剥离标签,也许this 答案可能会有所帮助?
  • 谢谢@Wubbler。我也在考虑将正则表达式作为可能的方式。将尝试实施它。
  • 你看过他们的脚本 API 吗?没有从对象中获取原始文本的方法吗?你是如何填充对象的?
  • 奥斯汀,是的,感谢您的参与。我在回答中提到了这一点。

标签: c# string unity3d character richtext


【解决方案1】:

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

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

【讨论】:

  • 感谢您的回答。尽管这将是实现它的正确方法,但我想必须扩展正则表达式模式以包含所有期望。除此之外,它会起作用。
【解决方案2】:

解决方案

对于非统一开发人员

据我了解,您将不得不使用正则表达式来解决它。像@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
}

谢谢大家的时间。干杯。

【讨论】:

    猜你喜欢
    • 2015-02-06
    • 2012-09-09
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 2017-07-16
    • 1970-01-01
    相关资源
    最近更新 更多