3

Platform: Unity 2018.2.20f1 Language: C# .Net 4.x

Scenario

I have a string which I would apply to a textmesh rendered​ in Unity. This rendering pipeline supports Rich text editing. http://digitalnativestudios.com/textmeshpro/docs/rich-text/

Request

Hence when I supply in a string, I would like to know character count which would exclude the characters used for Rich Text formatting.

Example

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

So what is the best way to implement it? Is there a module/resources online that would assist me in extracting the count?

Thank you, Karsnen

Filip
  • 155
  • 2
  • 14
  • 2
    Stripping the tags with regex comes to mind, perhaps [this](https://stackoverflow.com/a/18154046/7703386) answer might be helpful? – Wubbler Jan 08 '19 at 22:40
  • Thank you @Wubbler. I was also thinking about regex as the possible way. Will try to implement it. – Filip Jan 08 '19 at 22:54
  • Have you looked at their scripting API? Is there not a method to get raw text from the object? How are you populating the object? – Austin T French Jan 09 '19 at 18:11
  • Austin, yes and thank you for chiming in. I have mentioned that in my answer. – Filip Jan 09 '19 at 21:09

2 Answers2

1

You can set how it replaces the formatting block.

string CountNumber = Regex.Replace(richtextbox, "<.*?>", String.Empty);
MessageBox.Show(CountNumber.Length);
Yakov .P
  • 88
  • 8
  • Thank you for your answer. As much that would be the right way to implement it, I guess the regex pattern has to be expanded to be inclusive of all expectations. besides that, it will work. – Filip Jan 08 '19 at 23:51
0

Solution

For non-unity developers

From my understanding, you will have to use the Regex expressions to solve it. Something like @Wubber && @Yakov have suggested. Another answer with suggested by Wubber

For Unity Devs using TMPro.TextMeshProUGUI

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
}

Thank you for your time, everyone. Cheers.

Filip
  • 155
  • 2
  • 14