-2

How can I return the number of the word in text onclick or tap?

I was thinking of using Find.HitHighlight Method (Word) - MSDN(or something similar) but I don't how. For now I can count words in the text I have and store them in collection but how can I now know which one was clicked or taped so it can return me number of that word in collection and highlight it.

Thanks a lot!

here is code for WordCount:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Text.RegularExpressions;

public class WordCount : MonoBehaviour {
public Text textToCount;

// Use this for initialization
void Start () {
    Debug.Log(CountWords1(textToCount.text));
}

// Update is called once per frame
void Update () {

}

public static int CountWords1(string s)
{
    MatchCollection collection = Regex.Matches(s, @"[\S]+");
    return collection.Count;
    }
}
Brent Worden
  • 10,624
  • 7
  • 52
  • 57
Nikita Chernykh
  • 270
  • 1
  • 4
  • 18

1 Answers1

1

What you should do is use a dictionary where each word is the key value.

public Dictionary<string, int> AnalyzeString(string str)
{
    Dictionary<string,int> contents = Dictionary<string,int>();
    string[] words = str.Split(' ');
    foreach(string word in words)
    {
        if(contents.ContainsKey(word))
        {
            contents[word]+=1;
        }
        else
        {
            contents.Add(word,1);
        }
    }
    return contents;
}

With this you can now see how many times the queried word is in the string. just by doing

int numberOfTimes = 0;
if(contents.ContainsKey("yourDesiredWord"))
    numberOfTimes = contents["yourDesiredWord"];
Russ W.
  • 38
  • 4