Posting this solution because I don't think any of the previous solutions actually work.
Using this input
var sampleText =
"Here are some sentences. Is this a question? It certainly is! What other ways can end a sentence? A colon perhaps: or a semi-colon; Is this a question? Duplicated";
You can use string.Split() to separate the sentences, then feed that same array into another Split to determine which delimiter was used for each sentence
var x = sampleText.Split(new[] { '?', '!', '.', ';', ':' }, StringSplitOptions.RemoveEmptyEntries);
var y = sampleText.Split(x, StringSplitOptions.RemoveEmptyEntries);
var newText = new StringBuilder("");
for(var i = 0; i < y.Length; i++)
{
if (y[i] != "?")
{
newText.Append(x[i]);
newText.Append(y[i]);
}
}
Note in my sampleText I didn't terminate the last sentence properly - this will cause an exception unless you add
if (x.Length > y.Length)
newText.Append(x[x.Length - 1]);
Your comment
Quote:
i want to remove the whole question line dear.
might imply that these things are individual lines in a file (To get accurate answers you should take care in how you word your questions).
In this case I used a sample file containing
This is a statement.
This is another statement!
Is this is a question?
Are you sure that is a question?
That was a question
In which case this works (uses Linq)
var f = File.ReadAllLines(@"C:\Temp\Test.txt");
f = f.Where(p => (!string.IsNullOrEmpty(p) && !p.Contains("?"))).ToArray();
If I dump the contents of the array
f
to the console window I get
This is a statement.
This is another statement!
That was a question
If you are using an earlier version of C# and cannot use Linq then this works instead
var f1 = new List<string>();
foreach (var s in f)
{
if (!string.IsNullOrEmpty(s) && !s.Contains("?"))
f1.Add(s);
}
f = f1.ToArray();