Splitting a string at all whitespace

前端 未结 7 2111
刺人心
刺人心 2021-01-17 11:06

I need to split a string at all whitespace, it should ONLY contain the words themselves.

How can I do this in vb.net?

Tabs, Newlines, etc. must all be split

7条回答
  •  耶瑟儿~
    2021-01-17 11:08

    If you want to avoid regex, you can do it like this:

    "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
        .Split()
        .Where(x => x != string.Empty)
    

    Visual Basic equivalent:

    "Lorem ipsum dolor sit amet, consectetur adipiscing elit" _
        .Split() _
        .Where(Function(X$) X <> String.Empty)
    

    The Where() is important since, if your string has multiple white space characters next to each other, it removes the empty strings that will result from the Split().

    At the time of writing, the currently accepted answer (https://stackoverflow.com/a/1563000/49241) does not take this into account.

提交回复
热议问题