Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
need some assistance with removing sentences that ends with "?" or replacing it with nothing.
Posted
Comments
Abhipal Singh 28-May-15 5:07am    
Do you want to remove the entire question line? or you just want to remove "?" character?
Asking this question because the some solutions posted below does not align with the question.

Can you post a sample in your question of what is there in the file and what do you expect it to be?
Asad_Iqbal 29-May-15 6:33am    
i want to remove the whole question line dear.
Dianne Ramos 29-May-15 0:10am    
He's a troll. Wait till he answers his own question.
Asad_Iqbal 29-May-15 6:38am    
Dianne if u can't help then plz don't comment. i think this forum is not for irrelevant arguments.
CHill60 29-May-15 8:03am    
I disagree with the "troll" He's only answered 4 of his own posts in the last 5 years and has not "accepted" any of his own solutions. 2 of those "solutions" were just confirmation of how the problem was eventually solved - a mistake made by many inexperienced members. Another was a solution posted instead of updating the original post - a mistake made by many inexperienced members.
He could do with providing (much) more detail on what he's tried for sure but, again, he's not the only one guilty of that unfortunately.

Posting this solution because I don't think any of the previous solutions actually work.

Using this input
C#
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
C#
//Determine the individual sentences
var x = sampleText.Split(new[] { '?', '!', '.', ';', ':' }, StringSplitOptions.RemoveEmptyEntries);
//You may need to add further punctuation

//Split the text again using the sentences to determine which delimiter was used
var y = sampleText.Split(x, StringSplitOptions.RemoveEmptyEntries);

//Rebuild the paragraph ignoring any sentences terminated with question marks
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
C#
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)
C#
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
C#
var f1 = new List<string>();
foreach (var s in f)
{
    if (!string.IsNullOrEmpty(s) && !s.Contains("?"))
        f1.Add(s);
}
f = f1.ToArray();
 
Share this answer
 
Comments
Asad_Iqbal 29-May-15 13:02pm    
Thanks Alot @CHIll60 for ur help (y) :) :)

but it worked for small file but when i test it on large file i got this exception

Index was outside the bounds of the array.
newText.Append(x[i]);
in this line
Abhipal Singh 29-May-15 13:56pm    
I don't think file size will lead to this type of issue
Asad_Iqbal 29-May-15 14:08pm    
but it does i tested it.
CHill60 31-May-15 12:29pm    
This is going to be related to the actual content of the file - it would seem that y[] has a greater length than x[]. Where I suggested using StringSplitOptions.RemoveEmptyEntries it might be better to use StringSplitOptions.None instead - see if that fixes the problem
Maciej Los 29-May-15 16:52pm    
5ed!
Use Regex : The 30 Minute Regex Tutorial[^]
.+\?
 
Share this answer
 
Check out find string starts with and ends with.. it might help you.
 
Share this answer
 
1. string.Replace("?", string.Empty)
 
Share this answer
 
Comments
CHill60 28-May-15 5:20am    
If you note the question and the comments to Solution 1 you will realise that the OP wants to remove the entire sentence, not just the question mark.
Ralf Meier 29-May-15 7:00am    
the Solution 1 is no longer existant - why ?
CHill60 29-May-15 7:04am    
Either it was deleted by the Poster (some do when they realise the solution doesn't fit) OR it received enough reports of being "Off-Topic" or "Inaccurate" that it was automatically deleted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900