是否要计算字符串中的字符(不包括富格文本格式)?(C#)
平台:Unity 2018.2.20f1 语言:C#.Net 4.x 脚本 我有一个字符串,我将应用于textmesh渲染 团结一致。此呈现管道支持富文本编辑。 要求 因此,当我在字符串中提供时,我想知道字符数,这将排除用于富文本格式的字符 例子是否要计算字符串中的字符(不包括富格文本格式)?(C#),c#,string,unity3d,character,richtext,C#,String,Unity3d,Character,Richtext,平台:Unity 2018.2.20f1 语言:C#.Net 4.x 脚本 我有一个字符串,我将应用于textmesh渲染 团结一致。此呈现管道支持富文本编辑。 要求 因此,当我在字符串中提供时,我想知道字符数,这将排除用于富文本格式的字符 例子 string\u value=“我们绝对不感兴趣”; //字符计数应为29而不是43 那么,实施它的最佳方式是什么?是否有在线模块/资源可以帮助我提取计数 谢谢,, Karsnen您可以设置它如何替换格式块 string CountNumber
string\u value=“我们绝对不感兴趣”;
//字符计数应为29而不是43
那么,实施它的最佳方式是什么?是否有在线模块/资源可以帮助我提取计数
谢谢,,
Karsnen您可以设置它如何替换格式块
string CountNumber = Regex.Replace(richtextbox, "<.*?>", String.Empty);
MessageBox.Show(CountNumber.Length);
string CountNumber=Regex.Replace(richtextbox,”,string.Empty);
MessageBox.Show(CountNumber.Length);
解决方案
针对非unity开发者
根据我的理解,您必须使用正则表达式来解决它。类似于@Wubber&&@Yakov的建议。
用于使用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
}
谢谢大家抽出时间。干杯 我想到了用正则表达式剥离标签,也许答案会有帮助?谢谢@Wubbler。我还认为regex是一种可能的方式。将尝试实现它。您看过他们的脚本API吗?没有从对象获取原始文本的方法吗?你是如何填充这个对象的?奥斯汀,是的,谢谢你的来电。我在回答中提到了这一点,谢谢你的回答。尽管这是实现它的正确方式,但我想正则表达式模式必须扩展到包含所有期望。除此之外,它还会起作用。
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
}