Writing to a text file by trimming certain characters using C #?

Here I write to a text file. I am looking for a keyword Raiserrorin an sql file, and if this keyword exists, I must write this line.

He writes how

LineNo : 66 :  raiserror ('Sequence number cannot be blank',16,1)

But I need to write only

 LineNo : 66 : Sequence number cannot be blank

Is it possible. If so, how to crop .. Here is my code

while ((line = file.ReadLine()) != null)
{
    if (line.IndexOf("Raiseerror", StringComparison.CurrentCultureIgnoreCase) != -1)
    {
        dest.WriteLine("LineNo : " + counter.ToString() + " : " + "           " + line.TrimStart());
    }
    counter++;
}

file.BaseStream.Seek(0, SeekOrigin.Begin);
counter = 1;

Any suggestion?

+3
source share
4 answers

Try using this

int errorIndex = line.IndexOf("Raiseerror", StringComparison.CurrentCultureIgnoreCase)
if (errorIndex  != -1)
{
  int start = line.IndexOf('\'', errorIndex) + 1;
  int finish = line.IndexOf('\'', start);
  dest.WriteLine("LineNo : " + counter.ToString() + " : " + "           " + line.Substring(start,finish - start));
}
+2
source

The easiest way is to use the regex expression to capture. You should do something like this:

string pattern = @"raiserror \(\'(?<errortext>.+)\'";
Regex exp = new Regex(pattern);
Match match = exp.Match(line);
string errorText = match.Groups["errortext"].Value;

, , , . , , , , .

, , (, , ..).

+2

, LineNo: 66: ', :

private string GetText(string text)
{
    return string.Format("{0} {1}", text.Substring(0, text.LastIndexOf(":") + 1), text.Substring(text.IndexOf("'") + 1, (text.LastIndexOf("'") - text.IndexOf("'")) - 1));
}

var text = GetText("LineNo : 66 :  raiserror ('Sequence number cannot be blank',16,1)");

"LineNo: 66: "

[]

text.Substring(text.IndexOf("'") + 1, (text.LastIndexOf("'") - text.IndexOf("'")) - 1)

, .

!

+1

100%, ... , ? , .

var logLine = "LineNo : " + counter.ToString() + " : " + "           ";
dest.WriteLine(logLine.Trim());
0

All Articles