Exemplo n.º 1
0
        public static (int, string) GetWordIndexPair(string text, int charIndex, SearchCharType type = SearchCharType.None)
        {
            var wIndex = GetWordIndex(text, charIndex, type);

            //Debug.Log(wIndex);
            return(wIndex, text.Split(SplitPattern, StringSplitOptions.RemoveEmptyEntries).ElementAtOrDefault(wIndex));
        }
Exemplo n.º 2
0
        //A word is a string or char between white spaces or new lines or null chars
        public static int GetWordIndex(string text, int charIndex, SearchCharType type = SearchCharType.None)
        {
            var wIndex           = 0;
            var whiteSpacesCount = -1;
            var exit             = false;

            for (int i = 0; i < text.Length; i++)
            {
                if (charIndex == i)
                {
                    break;
                }

                while (text.ElementAtOrDefault(i).IsValidChar())
                {
                    i++;
                    whiteSpacesCount = 0;

                    if (charIndex == i)
                    {
                        exit = true;
                    }
                }

                if (exit)
                {
                    break;
                }

                whiteSpacesCount++;

                if (whiteSpacesCount == 1)
                {
                    wIndex++;
                }
                else
                {
                    whiteSpacesCount = -1;
                }
            }

            if (type != SearchCharType.None)
            {
                var value = type == SearchCharType.Right ? 1 : -1;

                if (!text.ElementAtOrDefault(charIndex + value).IsValidChar())
                {
                    wIndex = -1;
                }
            }

            return(wIndex);
        }