Search code examples
c#stringsplittamil

Other language word/string split/manipulation issue


Highlight the text searched scenario:

Ex: If I have a word RK and 'r' is searched, I have to highlight first occurance of 'r' i.e., RK. In the background it is like

< b >R< /b >K.

Similarly I have to highlight ம in மொ. Hence I am trying to find the position of ம in மொ and performing highlighting operation. Here I am getting the text after manipulation as

< b >ம< /b >ொ and hence it is displayed as

The code that I used for string manipulation and highlighting:

formattedString = string.Empty;
searchStringLength = searchString.Length;

formattedString += inputString.Substring(0, find);
formattedString += "<b>" + inputString.Substring(find, searchStringLength) + "</b>";
formattedString += inputString.Substring(find + searchStringLength);

The example is just for Tamil word, any suggestions to make it work for all other languages other than english?


Solution

  • I do not know Tamil. Looking at your question, the input string should be three letter string.

    Probably, you are setting your find variable something like

    find = inputString.IndexOf("ம");?
    

    somewhere in your code.

    The Tamil word மொ is not being counted as three letter word. Visual Studio is handling it as single letter while மொ.Length returns 2. ToCharArray() also returns array of two characters. That is why, IndexOf is always returning 0.

    Your comment on question:

    since ம + ொ = மொ, the ம find was returning true always. Now after this solution, ம find will return false and hence I don't have to highlight. Only if மொ is entered to find, it matches exactly and I can highlight.

    I do not think problem is in SubString. The IndexOf needs to be handled tactically.