remove the last occurrence of certain words

Anjali Agarwal 1,531 Reputation points
2023-12-14T06:23:17.7266667+00:00

I have the follwoing string

 

 

"Nominated By Anjali Agarwal <br /><br />This is test by Anjali<br /><br />Nominated By  Anonymous <br /><br />This is test by anonymous<br /><br />Nominated By  Anonymous <br /><br />"

How can I remove the last sentence "Nominated By Anonymous" from the above string. I only want to remove the last occurance of "Nominated By Anonymous". I dont want to remove the previous "Nominated By Anonymous" from the string. Also, Anonymous can be replaced by different words in the string so it can be "Nominated By Anonymous" or "Nominated By Anjali" or "Nominated By" certain name.

any help will be appreciated


Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-12-14T07:03:07.5566667+00:00

    Hi @Anjali Agarwal , Welcome to Microsoft Q&A,

    If all your characters are in the same format.

    You can use the LastIndexOf method to find the last occurrence, and then use Substring to intercept the required part.

    using System;
    
    namespace xxx
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                string inputString = "Nominated By Anjali Agarwal <br /><br />This is test by Anjali<br /><br />Nominated By  Anonymous <br /><br />This is test by anonymous<br /><br />Nominated By  Anonymous <br /><br />";
    
                string phraseToRemove = "Nominated By";
    
                string modifiedString = RemoveLastOccurrence(inputString, phraseToRemove);
    
                Console.WriteLine(modifiedString);
                Console.ReadLine();
            }
            static string RemoveLastOccurrence(string inputString, string phraseToRemove)
            {
                int lastOccurrenceIndex = inputString.LastIndexOf(phraseToRemove);
    
                if (lastOccurrenceIndex != -1)
                {
                    string modifiedString = inputString.Substring(0, lastOccurrenceIndex);
    
                    // Trim trailing newline if present
                    if (modifiedString.EndsWith("\n"))
                    {
                        modifiedString = modifiedString.Substring(0, modifiedString.Length - 1);
                    }
    
                    return modifiedString;
                }
                else
                {
                    return inputString;
                }
            }
        }
    }
    
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.