开发者

Removing text using regular expressions [duplicate]

This question already has answers here: Remove a piece of text using Regex in C# .NET (4 answers) Closed 9 years ago.

I want to remove "2011" and everything before it plus a single space (" ") after "2011".

Not quite sure how to approach this, all I could think of is a simple regex-like find and replace.

string temp;
StringBuilder sb = new StringBuilder();
string[] file = File.ReadAllLines(@"TextFile1.txt");
foreach (string line in file)
{
    if (line.Contains("  "))
    {
        temp = line.Replace("  ", " ");
        sb.Append(temp + "\r\n");
        continue;
    }
    else
        sb.Append(line + "\r\n");
 }
 File开发者_如何学Go.WriteAllText(@"TextFile1.txt", sb.ToString());

I have modified my code and with a bit of luck got it to work. Modifications look as follows:

if (line.Contains("2011"))
   {

   temp = line.Substring(line.IndexOf("2011 ") + 5);


   sb.Append(temp + "\r\n");

   continue;

   }


string s = "653 09-23-2011 21 27 32 40 52 36 ";
s = s.Substring(s.IndexOf("2011 ") + 5);

The result is that s = "21 27 32 40 52 36 "

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜