Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.43/5 (6 votes)
See more:
Hi Codeproject World !
I wanna have something like this exp:

this is my sentence for exp:
In both test settings, the array of string (1,000,000 items) was pre-populated, thus the stopwatch counts only elapsed time spent on actual string concatenation (please note, that in original article the loop includes i.ToString() conversion, which is included in elapsed time estimates, thus affecting the result and distorting the performance statistics). Also, a stopwatch object provides much more accurate time measurement (it is a part of System.Diagnostics object library).

Now I wanna find 'o'+'t' in the above sentence.
Result :
both
populated
counts
concatenation
note
ToString()
distorting
stopwatch
object
System.Diagnostics


How can i do that in C# ?
Tanks in advanced!
Posted
Comments
Sergey Alexandrovich Kryukov 24-Sep-12 0:15am    
What did you try so far?
--SA
_Amy 24-Sep-12 1:03am    
Are you looking for regular expression to split these kind of string?

You can use Regular Experessions:
http://en.wikipedia.org/wiki/Regular_expression[^]

Use the class System.Text.RegularExpressions.Regex:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[^].

Good luck,
—SA
 
Share this answer
 
Comments
jojoba2011 24-Sep-12 0:46am    
Your Solution is Good.But i cannot create the pattern.
Plz Help!
pasztorpisti 24-Sep-12 5:34am    
Your regex pattern to search for with your example should be something like "[^ \t.]*o[^ \t.]*t[^ \t.]*" where "[^ \t.]*" is the inverse set of characters that can be used as separators. If you can not create the pattern then learn some regex, otherwise you will never be able to create the pattern of course.
pasztorpisti 24-Sep-12 5:31am    
+5, I think this is the solution to go with.
Sergey Alexandrovich Kryukov 24-Sep-12 12:22pm    
Thank you very much, especially for the help with patterns.

Perhaps we can help OP more about learning the art of Regex pattern and this particular patterns.
Besides, it can be simpler if combined with string.Split, but I would rather recommend doing it all in Regex, which is possible, needs just a bit thinking and experience.

--SA
Use regular expression to break the sentence to words. And from that word you can find the word which contains "o" or "t".
You can try this:
C#
string input = "Your sentence here";
string[] sentences = Regex.Split(input, @"(?<=[\.!\?])\s+");
foreach (string sentence in sentences) {
    if (sentence.Contains("o") || sentence.Contains("t"))
          Console.WriteLine(sentence);
}



--Amit
 
Share this answer
 
v2
Comments
pasztorpisti 24-Sep-12 5:28am    
This doesn't handle the order of 'o' and 't' so I have to vote 4.
C#
public void getChars(string strWords)
        {
            char[] chrArrSep={' ',','};
            string[] strArrWords = strWords.Split(chrArrSep,StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in strArrWords)
            {
                if (s.Contains('o'.ToString()) && s.Contains('t'.ToString()))
                {
                    Console.WriteLine(s);
                }
            }
        }


Hope this will solve your problem.
 
Share this answer
 
Comments
jojoba2011 24-Sep-12 0:46am    
The first solution is perfect but i can create the pattern.
Plz Help !
pasztorpisti 24-Sep-12 5:31am    
Have my 4, your solution doesn't handle the order of 'o' and 't'.
[no name] 24-Sep-12 15:09pm    
You didn't mention to the order. You just said 'o' + 't'.
pasztorpisti 24-Sep-12 15:28pm    
Indeed, I've checked out the question and it isn't clear whether the order is defined or not, the OP should clarify it.
C#
string sentence = "your sentence...";

string[] words = sentence.Split(' ');

foreach (string str in words)
{
    if (str.Contains("o") && str.Contains("t"))
    {
         int indexO = str.IndexOf('o');
         int indexT = str.IndexOf('t');

         if (indexT > indexO)
            Console.WriteLine(str);
    }
}
 
Share this answer
 
v3
Comments
pasztorpisti 24-Sep-12 5:28am    
This handle neither the order of 'o' and 't' nor the special separator chars so I have to vote 3.
[no name] 24-Sep-12 15:14pm    
See my changes.
pasztorpisti 24-Sep-12 15:31pm    
What about making just 1 or t2 IndexOf() calls without Contains? IndexOf returns -1 if there is no occurrance and this way you can skip the call even to a second IndexOf if the first returned -1. Thats more efficient, but its already OK.
[no name] 25-Sep-12 6:12am    
Your idea is correct too, but count of conditions won't reduce. Am I right?
pasztorpisti 25-Sep-12 6:18am    
Still I don't understand why not to make 1 or 2 function calls instead of 4 when the worst case is iterating through the whole string with each call.
C#
public string[] getWords(string strWord)
{
    char[] chrCompare={' ',',','-'};

    string[] strWords = strWord.Split(chrArrSep);

    var strFilteredWords = (from Word in strWords.AsEnumerable()
                            where Word.Contains("o") && Word.Contains("t")
                            select Word).ToArray();
    return strFilteredWords; 
}
 
Share this answer
 

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