Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I wrote a program to highlight a text in a list of words. For example "The boy is a good", if i want to highlight "boy" using indexOf, it will be highlighted. now the problem is for a statement like "The boy is a good boy", if i want to highlight the second boy, i dont know how, because its only the first boy that will still be highlighted. Pls help as am still some how new to c sharp.
Posted
Comments
Zoltán Zörgő 29-Aug-13 5:32am    
What is your problem:
- you can not find both "boy"'s starting index or
- you don't know how to highlight both non-continuous characters
How exactly do you look for matching substring, and ho exactly do you highlight in what sort of control?
Member 9769841 29-Aug-13 5:44am    
Its a RichtextBox and may be when you see the codes, you will understand me. Its actually a application that compares the text in RichtextBox1 to the text in RichtextBox 2. It then highlights the diffrent portion in richtextBox2. Have been able to do that, except that it highlights the first occurence of the diffrent text. Here is the code....


public void check()
{
string[] RichTextBoxLines1 = dragDropclass1.Lines;
string[] RichTextBoxLines2 = dragDropclass1.Lines;

List<string> Test1 = new List<string>(dragDropclass1.Text.Split(new char[] {' '}));
List<string> Test2 = new List<string>(dragDropclass2.Text.Split(new char[] {' '}));

if (Test1.Count > Test2.Count)
{
for (int i = 0; i <= Test1.Count - Test2.Count; i++)
{
Test2.Add(" ");
}
}
else if (Test2.Count > Test1.Count)
{
for (int i = 0; i <= Test2.Count - Test1.Count; i++)
{
Test1.Add(" ");
}
}



bool[] value = new bool[Test1.Count];
for (int i = 0; i < Test1.Count; i++)
{
string a = Test1[i];
string b = Test2[i];
value[i] = string.Equals(a, b, StringComparison.Ordinal);

}


for (int i = 0; i < value.Length; i++)
{
if (value[i] == false)
{
int k = i + (i - 1);

// int s = dragDropclass2.Text.IndexOf(Test2[i], Test2[i].IndexOf(Test2[i]),Test2[i].Length);
dragDropclass2.SelectionStart = dragDropclass2.Text.IndexOf(Test2[i],k);
dragDropclass2.SelectionLength = Test2[i].Length;
dragDropclass2.SelectionColor = Color.Red;

}


}

Test1.Clear();
Test2.Clear();

}

Where Dragdropclass are the control i created to enable me drag text file into the richtextBox.
lukeer 29-Aug-13 7:59am    
Please move that code to the question. Use the "Improve question" link for that.
And wrap code in tags like these: <pre lang="c#">YourCodeHere();</pre>
That way it will be nicely formatted and therefore readable.

Two options:

using String.IndexOf:
C#
int SecondIndexOf(string theString, string toFind)
{
    int first = theString.IndexOf(toFind);

    if (first == -1) return -1;

    // Find the "next" occurrence by starting just past the first
    return theString.IndexOf(toFind, first + 1);
}

Using Regex:
C#
public static int NthIndexOf(this string target, string value, int n)
    {
        Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}");

        if (m.Success)
            return m.Groups[2].Captures[n - 1].Index;
        else
            return -1;
    }


Cheers,
Edo
 
Share this answer
 
v2
Comments
Member 9769841 29-Aug-13 5:50am    
Thanks..for the advice, i will really use that next time
Joezer BH 29-Aug-13 6:00am    
Why "next time"? did you solve it in a different way?
Member 9769841 29-Aug-13 7:37am    
i meant that i will make use of the comment box to reply to answers; like you told me to
Joezer BH 29-Aug-13 7:40am    
Ah, yes for got about that msg...

:)
First You Have To Check Condition That How Many Times That Word Is In That Row When Its Find Then Give Index Of It....
 
Share this answer
 
Comments
Member 9769841 29-Aug-13 5:37am    
The thing is, The text are dynamic text, and it might even actually be numerous of lines of text...
What am working on is something that identifies the diffrent portion of another text. have been able to do that. But instead of highlighting the Text at the exact position, it highlights the first occurrence of that Text.
Mithiten 29-Aug-13 5:49am    
String Comparison constants result in the same value from the IndexOf method. The String Comparison enum is important
Perhaps this could help. This function locates all the occurrences of the text you're looking for:
XML
private List<Match> FindOccurrences( string SourceText, string Expression )
{
    List<Match> Result = new List<Match>();

    Regex Parser = new Regex( Expression, RegexOptions.Compiled );

    Match Match = Parser.Match( SourceText );

    while (Match.Success)
    {
        Result.Add( Match );
        Match = Match.NextMatch();
    }

    return Result;
}


You can call the function simply like this:
List<Match> Matches = FindOccurrences( "This contains boy here and again contains boy here", "boy" );


The property Index of the objects of type Match contains the position into the input string.
 
Share this answer
 
Comments
Member 9769841 29-Aug-13 7:50am    
Thanks V,Lorz...but is there a way to make sure i only highlight the nth(may be even the fourth boy in the text)
V.Lorz 29-Aug-13 8:03am    
Yes, modify the function (or create a new one) so you can count matches until the nth match (or no more matches) is found. Ugly, but works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900