public static string RegexMethod(string stroka) { int counter = 0; Regex regex = new Regex(@"\w[\s\,\w]*[.!?]"); MatchCollection collection = regex.Matches(stroka); int[] countWords = new int[collection.Count]; foreach (Match sentence in collection) { //Word counter int amount = 1; //Getting char array from a sentece var arrayBuf = sentence.Value.ToCharArray(); for (int j = 0; j < arrayBuf.Length; j++) { if (arrayBuf[j] == ' ' && arrayBuf[j - 1] != ' ') { amount++; } } countWords[counter++] = amount; } return(collection[MaxValueIndex.IndexOfMaxValue(countWords)].Value); }
public string StringMethod(string stroka) { // Split the string into a sentences var stArray = stroka.Split(new char[] { '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries); // Array for counting words in sentences int[] countWords = new int[stArray.Length]; // Loop for finging the number of words in sentences for (int i = 0; i < stArray.Length; i++) { //Trimming spaces around the edges of one sentence stArray[i] = stArray[i].Trim(); //Word counter int amount = 1; //Getting char array from a sentece var arrayBuf = stArray[i].ToCharArray(); for (int j = 0; j < arrayBuf.Length; j++) { if (arrayBuf[j] == ' ' && arrayBuf[j - 1] != ' ') { amount++; } } countWords[i] = amount; } return(stArray[MaxValueIndex.IndexOfMaxValue(countWords)]); }