0

I want to compare two string. Is there existing diffrences in each index, i want to color that specific char in red (if not match) or green (if it match).

            <li>
                @for (int i = 0; i < item.AnswersList.Count; i++)
                {
                    var answerArray = item.AnswersList[i].Output.ToArray();
                    var outputArray = item.OutputList[i].Output.ToArray();

                    for (int j = 0; j < answerArray.Length; i++)
                    {
                        if (answerArray[j] == outputArray[j])
                        {
                            @Html.Raw( How to print colorful char here?);
                        }
                    }
                }
            </li>

That colored string need to be in one line in <li> tag.

EDIT:

I made this edit to my code:

<div style="color: green;">
     @Html.Raw(outputArray[j])
</div>

But i get output like that:

enter image description here

How to make it one word in <li> tag?

3 Answers 3

1

Try the following:

<li style="display: flex;>
   @for (int i = 0; i < item.AnswersList.Count; i++)
   {
      var answerArray = item.AnswersList[i].Output.ToArray();
      var outputArray = item.OutputList[i].Output.ToArray();

      for (int j = 0; j < answerArray.Length; i++)
      {
         if (answerArray[j] == outputArray[j])
         {
            <span style="color: green;">
              ...
            </span>
         }
      }
   }
</li>
Sign up to request clarification or add additional context in comments.

7 Comments

But it will print every char in another <li> tag. i want to have string in one <li> tag with each char coloured
Sure, it works nice, but <span> gives me whitespace for each character. Could My arrays have whitespaces, strings etc so span is unacceptable because each span gives me whitespace between
if span is giving you extra margin or padding, adapt your css rules. Div containers are block types, while spans are displayed inline. The styling is up to you. Fire up the dev console, find the rule, that is adding the behaviour and adapt or override ut.
I can pack this for in div, but how css to no whitespaces between spans will look like?
I have edited my answer with <li style="display: flex;">
|
1

I hope you need this:

       <li>
            @for (int i = 0; i < item.AnswersList.Count; i++)
            {
                var answerArray = item.AnswersList[i].Output.ToArray();
                var outputArray = item.OutputList[i].Output.ToArray();

                for (int j = 0; j < answerArray.Length; i++)
                {
                    bool isMatch = answerArray[j] == outputArray[j];
                    <div style="color: @(isMatch ? "green" : "red");">
                        @Html.Raw(outputArray[j])
                    </div>
                }
            }
        </li>

UPD: If you need all chars in one row then use span instead of div.

UPD2: Try this(span but without new line):

<span style="color: @(isMatch ? "green" : "red");margin:0;padding:0">@Html.Raw(outputArray[j])</span>

2 Comments

span gives me extra whitespace betwean each span. In my char array i have words, spaces etc. Span makes space foreach char. How to deal with that?
One way to handle is to add css styling for your divs: ul>li>div: display: inline-block;
1

This should work perfectly. It's just a matter of making sure you don't leave any whitespace around your <span> tags, like this:

<li>
    @for (int i = 0; i < item.AnswersList.Count; i++)
    {
        var answerArray = item.AnswersList[i].Output.ToArray();
        var outputArray = item.OutputList[i].Output.ToArray();

        for (int j = 0; j < answerArray.Length; i++)
        {
            if (answerArray[j] == outputArray[j])
            {<span style="color:green">@Html.Raw(outputArray[j])</span>}
            else
            {<span style="color:red">@Html.Raw(outputArray[j])</span>}
        }
    }
</li>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.