You could use a regex :) return Regex. Matches(hello, "hello"). Count == 2; This matches the string hello for the pattern "hello" and returns true if the count is 2.
Regular expressions. If (Regex. IsMatch(hello,@"(.*hello.
*){2,}")) I guess you meant "hello", and this will match a string with at least 2 "hello" (not exactly 2 "hello").
1 for the matching with a count regex :-) – pjvds Nov 8 at 16:16 thx for answer, wil try it tomorrow – eMi Nov 8 at 17:24.
Contains static text methods. /// Put this in a separate class in your project. /// public static class TextTool { /// /// Count occurrences of strings.
/// public static int CountStringOccurrences(string text, string pattern) { // Loop through all instances of the string 'text'. Int count = 0; int I = 0; while ((i = text. IndexOf(pattern, i))!
= -1) { I += pattern. Length; count++; } return count; } } Example usage: using System; class Program { static void Main() { string s1 = "Sam's first name is Sam. "; string s2 = "Dot Net Perls is about Dot Net"; string s3 = "No duplicates here"; string s4 = "aaaa"; Console.
WriteLine(TextTool. CountStringOccurrences(s1, "Sam")); // 2 Console. WriteLine(TextTool.
CountStringOccurrences(s1, "cool")); // 0 Console. WriteLine(TextTool. CountStringOccurrences(s2, "Dot")); // 2 Console.
WriteLine(TextTool. CountStringOccurrences(s2, "Net")); // 2 Console. WriteLine(TextTool.
CountStringOccurrences(s3, "here")); // 1 Console. WriteLine(TextTool. CountStringOccurrences(s3, " ")); // 2 Console.
WriteLine(TextTool. CountStringOccurrences(s4, "aa")); // 2 } } From: http://www.dotnetperls.com/string-occurrence.
You can use a regex, and check the length of the result of Matches function. If it's two you win.
If you use a regular expression MatchCollection you can get this easily: MatchCollection matches; Regex reg = new Regex("hello"); matches = reg. Matches("hellohelloaklsdhas"); return (matches. Count == 2).
– Vlad Nov 8 at 16:16 @Vlad: You don't. It was a holdover from a copy/paste.Thanks. – Joel Etherton Nov 8 at 16:18 Or you could just use the one liner static method.
– Ray Nov 8 at 16:19 There are a myriad number of ways to accomplish this task. I personally like @m0skit0's answer the best. I was tempted to point out that your first answer used .
Match instead of . Matches but I figured you'd catch on to it eventually :) – Joel Etherton Nov 8 at 16:26.
IndexOf You can use the IndexOf method to get the index of a certain string. This method has an overload that accepts a starting point, from where to look. When the specified string is not found, -1 is returned.
Here is an example that should speak for itself. Var theString = "hello hello bye hello"; int index = -1; int helloCount = 0; while((index = theString. IndexOf("hello", index+1))!
= -1) { helloCount++; } return helloCount==2; Regex Another way to get the count is to use Regex: return (Regex. Matches(hello, "hello"). Count == 2).
IndexOf: int FirstIndex = str. IndexOf("hello"); int SecondIndex = str. IndexOf("hello", FirstIndex + 1); if(FirstIndex!
= -1 && SecondIndex! = -1) { //contains 2 or more hello } else { //contains not } or if you want exactly 2: if(FirstIndex! = -1 && SecondIndex!
= -1 && str. IndexOf("hello", SecondIndex) == -1).
Public static class StringExtensions { public static int Matches(this string text, string pattern) { int count = 0, I = 0; while ((i = text. IndexOf(pattern, i))! = -1) { I += pattern.
Length; count++; } return count; } } class Program { static void Main() { string s1 = "Sam's first name is Sam. "; string s2 = "Dot Net Perls is about Dot Net"; string s3 = "No duplicates here"; string s4 = "aaaa"; Console. WriteLine(s1. Matches("Sam")); // 2 Console.
WriteLine(s1. Matches("cool")); // 0 Console. WriteLine(s2. Matches("Dot")); // 2 Console. WriteLine(s2. Matches("Net")); // 2 Console.
WriteLine(s3. Matches("here")); // 1 Console. WriteLine(s3. Matches(" ")); // 2 Console. WriteLine(s1. Ma3tches("aa")); // 2 } }.
1 for the matching with a count regex :-) – pjvds Nov 8 '11 at 16:16 thx for answer, wil try it tomorrow – eMi Nov 8 '11 at 17:24.
– Vlad Nov 8 '11 at 16:16 @Vlad: You don't. It was a holdover from a copy/paste.Thanks. – Joel Etherton Nov 8 '11 at 16:18 Or you could just use the one liner static method.
– Ray Nov 8 '11 at 16:19 There are a myriad number of ways to accomplish this task. I personally like @m0skit0's answer the best. I was tempted to point out that your first answer used .
Match instead of . Matches but I figured you'd catch on to it eventually :) – Joel Etherton Nov 8 '11 at 16:26.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.