C# – Remove String After Determinate String

cnet

I need to remove certain strings after another string within a piece of text.
I have a text file with some URLs and after the URL there is the RESULT of an operation. I need to remove the RESULT of the operation and leave only the URL.

Example of text:


http://website1.com/something                                        Result: OK(registering only mode is on) 

http://website2.com/something                                    Result: Problems registered 100% (SOMETHING ELSE) Other Strings; 

http://website3.com/something                               Result: error: "Âíèìàíèå, îáíàðóæåíà îøèáêà - Ìåñòî æèòåëüñòâà ñîäåðæèò íåäîïóñòèìûå ê 

I need to remove all strings starting from Result: so the remaining strings have to be:

http://website1.com/something

http://website2.com/something

http://website3.com/something

Without Result: ……..

The results are generated randomly so I don't know exactly what there is after RESULT:

Best Answer

One option is to use regular expressions as per some other answers. Another is just IndexOf followed by Substring:

int resultIndex = text.IndexOf("Result:");
if (resultIndex != -1)
{
    text = text.Substring(0, resultIndex);
}

Personally I tend to find that if I can get away with just a couple of very simple and easy to understand string operations, I find that easier to get right than using regex. Once you start going into real patterns (at least 3 of these, then one of those) then regexes become a lot more useful, of course.