Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:09:49+00:00 2026-06-10T02:09:49+00:00

My goal is to check if a string contains any string values from a

  • 0

My goal is to check if a string contains any string values from a list, store that value and wrap it in HTML-tags. I’m kind of lost here.

My List:

public List<string> AccessModifiers
{
    get { return _accessModifiers ?? (_accessModifiers = new List<string>()); }
}

With the values addes in a separate method:

AccessModifiers.Add("public");
AccessModifiers.Add("private");
AccessModifiers.Add("protected");
AccessModifiers.Add("internal");
AccessModifiers.Add("protected internal");
AccessModifiers.Add("void");

Let’s say i was sendind the text protected void TagListView_ItemDataBound(object sender, ListViewItemEventArgs e) to the code below, what I want is to get the keywords protected and void as matchedItems to wrap in my HTML-tags. It feels like the Find method should check if the item is found, not the whole list, but I’m a bit lost here. Here’s what I got so far:

        foreach (var item in AccessModifiers)
        {
            if (UsedItems == null) // If the session is null...
            {
                UsedItems = new List<string>(); // ...a new one is initiated
            }

            if(!UsedItems.Contains(item)) // Check if the iterated item is found in the session-list
            {
                bool match = AccessModifiers.Any(ArticleTextBox.Text.Contains);
                string matchedItem = AccessModifiers.Find(ArticleTextBox.Text.Contains);

                if (match == true)
                {
                    string openTag = "<span class='accessModifiers'>";
                    string closeTag = "</span>";
                    string newModifier = openTag + matchedItem + closeTag;
                    newText = newText.Replace(matchedItem, newModifier);
                    UsedItems.Add(matchedItem);  // Add the matchedItem (used item) to the session-list not to be iterated again
                }
            }
        }
        return newText;

I can at this point only get the keyword protected to be stored, not void. It seems like it would be more simple than what I’ve come up with so far.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:09:50+00:00Added an answer on June 10, 2026 at 2:09 am

    It seems like it would be more simple than what I’ve come up with so far.

    Quite the reverse. In particular, while the example you’ve given is simple, the general case isn’t:

    public static void thisvoidmethodisstatic()
    {
        Console.WriteLine("private within text");
    }
    

    Basically you’ll need code which understands the structure of code rather better. Simple search and replace isn’t going to cut it.

    The code highlighter I use on csharpindepth.com is based on a VB highlighter written by Jared Parsons. I’ll see whether I can find the original source – but if not, I’m sure there are other open source code highlighters available.

    EDIT: If you really are happy with just doing a naive search and replace, then you’re definitely making it more complicated than you need to.

    • You’re iterating over all the access modifiers and also looking for all of them in the loop
    • I would strongly recommend getting rid of the conditional list assignment – just always assign an empty list beforehand. Aside from anything else, it avoids you having to check for nullity elsewhere.

    Then you’ve just got:

    foreach (var modifier in AccessModifiers)
    {
        if (ArticleTextBox.Text.Contains(modifier))
        {
            UsedItems.Add(modifier);
            string openTag = "<span class='accessModifiers'>";
            string closeTag = "</span>";
            string newModifier = openTag + matchedItem + closeTag;
            newText = newText.Replace(matchedItem, newModifier);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal: Produce an Excel document with information from 3 associated models that is similar
The goal is to check if a string is a URL, no regex needed.
My ultimate goal is to do this programmatically, but as a sanity check I'm
Goal is to make a dialog that appears on menu_key pressed, but it keeps
Goal: to create a percentage column based off the values of calculated columns. Here's
The goal is to avoid copying the string data when I need a const
I wanted to check that typeid is evaluated at compile time when used with
I am not sure how to do this. The goal: I want to check
How would you implement that check? You have N parameters for a stored procedure.
Below is a function from a program: //read the specified file and check for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.