Как избежать разбиения слова на регистр - PullRequest
0 голосов
/ 30 мая 2020

Ниже приведен текст, который я хочу разделить:

string content = "Tonight the warning from the Police commissioner as they crack down on anyone who leaves there house without a 'reasonable excuse' 
(TAKE SOT)"

string[] contentList = content.split(' ');

Я не хочу разделять слово, т.е. (ПРИНЯТЬ СОТ), если текст в скобках и в верхнем регистре, то как чтобы избежать расщепления детали.

Спасибо

Ответы [ 2 ]

1 голос
/ 01 июня 2020

Метод split может иметь два параметра. Первый ограничивает подстроки в этом экземпляре. Второй - максимальное количество элементов, ожидаемых в массиве.

Следующий фрагмент кода работает на меня, вы может ссылаться на него.

string content = "Tonight the warning from the Police commissioner as they crack down on anyone who leaves there house without a 'reasonable excuse' (TAKE SOT)";
            string[] contents = content.Split(" ", content.Substring(0, content.IndexOf("(")).Split(" ").Length);

И это результат кода: enter image description here

Я использую метод Split (String, Int32, StringSplitOptions) enter image description here

enter image description here

0 голосов
/ 02 июня 2020

Позже, проведя небольшое исследование, я нашел решение проблемы:

public class Program
{
    public static void Main()
    {
        string BEFORE_AND_AFTER_SOUND_EFFECT = @"\(([A-Z\s]*)\)";
        List<string> wordList = new List<string>();
        string content = "Tonight the warning from the Police commissioner as they (VO) crack down on anyone who leaves there house without a 'reasonable excuse' (TAKE SOT) INCUE:police will continue to be out there.";
        GetWords(content, wordList, BEFORE_AND_AFTER_SOUND_EFFECT);

        foreach(var item in wordList)
        {
            Console.WriteLine(item);
        }           
    }

    private static void GetWords(string content, List<string> wordList, string BEFORE_AND_AFTER_SOUND_EFFECT)
    {
        if(content.Length>0)
        {
            if (Regex.IsMatch(content, BEFORE_AND_AFTER_SOUND_EFFECT))
            {
                int textLength = content.Substring(0, Regex.Match(content, BEFORE_AND_AFTER_SOUND_EFFECT).Index).Length;
                int matchLength = Regex.Match(content, BEFORE_AND_AFTER_SOUND_EFFECT).Length;
                string[] words = content.Substring(0, Regex.Match(content, BEFORE_AND_AFTER_SOUND_EFFECT).Index - 1).Split(' ');
                foreach (var word in words)
                {
                    wordList.Add(word);
                }
                wordList.Add(content.Substring(Regex.Match(content, BEFORE_AND_AFTER_SOUND_EFFECT).Index, Regex.Match(content, BEFORE_AND_AFTER_SOUND_EFFECT).Length));
                content = content.Substring((textLength + matchLength) + 1); // Remaining content
                GetWords(content, wordList, BEFORE_AND_AFTER_SOUND_EFFECT); 
            }
            else
            {
               wordList.Add(content);
               content = content.Substring(content.Length); // Remaining content
               GetWords(content, wordList, BEFORE_AND_AFTER_SOUND_EFFECT);
           }
        }
    }
}

Спасибо.

...