How to split text into words?

前端 未结 7 2334
-上瘾入骨i
-上瘾入骨i 2020-12-04 22:39

How to split text into words?

Example text:

\'Oh, you can\'t help that,\' said the Cat: \'we\'re all mad here. I\'m mad. You\'re mad.\'

相关标签:
7条回答
  • 2020-12-04 23:14

    Split text on whitespace, then trim punctuation.

    var text = "'Oh, you can't help that,' said the Cat: 'we're all mad here. I'm mad. You're mad.'";
    var punctuation = text.Where(Char.IsPunctuation).Distinct().ToArray();
    var words = text.Split().Select(x => x.Trim(punctuation));
    

    Agrees exactly with example.

    0 讨论(0)
提交回复
热议问题