基于对空白奇数分割字符串 [英] Splitting string based on uneven number of white spaces

查看:97
本文介绍了基于对空白奇数分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要拆分一个字符串,它看起来像这样

I need to split a string that looks like this

1052 root         0 SW<  [hwevent]

为以下

1052
root
0
SW<
[hwevent]

当然,我可以只掀起forloop和空格比较字符索引,当发生不是空白,发生添加到一个新的字符串数组,但我觉得这是一个非常肮脏的方式做到这一点

sure, I could just whip up a forloop and compare character indices with white spaces and when the occurrence is not a white space, add the occurrence to a new string array but I feel like this is a really dirty way to do this.

什么是一个很好的方式来分割这个字符串?正则表达式吧?

What is a good way to split this string? Regex perhaps?

推荐答案

您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.stringsplitoptions%28v=vs.100%29.aspx">StringSplitOptions.RemoveEmptryEntries

string strtemp = "1052 root         0 SW<  [hwevent]";
string[] array = strtemp.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

这篇关于基于对空白奇数分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆