C# how to remove a word from a string

Question

Wednesday, May 13, 2015 5:20 PM

 how do I  remove a word from a string ?I have a file saved to a string and I need to go though that string and remove one word at a time so I can process the information.

by remove I mean i need to save it to another string...

All replies (6)

Wednesday, May 13, 2015 6:45 PM âś…Answered

no that will not work. I have a whole big file in the string. I need to remove the words out one at a time.

So add a loop around it. You will have to iterate over your find/replacements, here is one possible way. If you want more help please provide code you have written or tried.

var replacements = new[]{
   new{Find="123",Replace="Word one"},
   new{Find="ABC",Replace="Word two"},
   new{Find="999",Replace="Word two"},
};

var myLongString = "123 is a long 999 string yeah";

foreach(var set in replacements)
{
   myLongString = myLongString.Replace(set.Find, set.Replace);
}
// or in linkvar myLongStringReplaced = replacements.Aggregate(myLongString, (current, set) => current.Replace(set.Find, set.Replace));
Console.WriteLine(myLongString);

Mark as answer or vote as helpful if you find it useful | Igor


Wednesday, May 13, 2015 6:12 PM

this is the easiest way:

         

             string first_string = "this is a test string";
            string second_string = string.Empty;
            if (first_string.Contains("test") )
            {
                 second_string = first_string.Remove(first_string.IndexOf(" test"), " test".Length);
            }
            if (second_string.Contains("Test"))
            {
                second_string = second_string.Remove(second_string.IndexOf("Test"), "test".Length);
            }
            Console.WriteLine(second_string);

            Console.ReadKey();

hope it helps.. u still have to check for test. / test? etc..


Wednesday, May 13, 2015 6:30 PM

Hi,

you can replace the word that you want to remove with a empty string like

string result ="this is a test string";
            result = result.Replace(" test", "");
            //Output "this is a string"

If this answer was helpful please remember to close your threads by marking helpful posts as answer

good luck :)

Fares


Wednesday, May 13, 2015 6:39 PM

no that will not work.

I have a whole big file in the string.

I need to remove the words out one at a time.

string temp "  text = 67"

loop

1st time in the loop

temp2 = text

then  2rd time in the loop

loop2

temp2 ==

then third time in the loop

temp2 = 67

like that


Wednesday, May 13, 2015 6:44 PM

hello,

sorry i have a hard time understanding this. can you provide  some code of what you have tried?


Wednesday, May 13, 2015 6:47 PM

You would first split your string to a string array of words by splitting from the space char.

string str = "Hello this is a sentence.";string[] word_arr = str.Split(' ');From there you can identify the word position usingint wordPos = str.IndexOf(word_arr[0]);

Remember however, your sentence may contains the same word twice. So if you want to find the second one, you should process the string by removing word entries from the sentence as you walk trough it

 

string str = "Hello this is a sentence.";


            string sentence = "Hello this is a sentence. Hello is present twice!";
            string wordToFind = "Hello";
            List<int> foundPos = new List<int>();

            for (int i = word_arr.Length - 1; i >= 0; i--)
            {
                var pos = sentence.IndexOf(wordToFind);

                sentence = sentence.Remove(0, wordToFind.Length);

                foundPos.Add(pos);
            }






}