楼上的方法是不行的。
根据MSDN, 实际上 Split() 等价于 Split(' ','\t','\n',...)
其中的 ... 表示其它Unicode空白字符。
可以用下面的代码验证:
class Test
{
static void Main()
{
foreach (string s in " a bc d ".Split(' '))
System.Console.Write("[{0}]", s);
}
}
结果是: [][a][][bc][d][]
但我希望是: [a][bc][d]
根据MSDN, 实际上 Split() 等价于 Split(' ','\t','\n',...)
其中的 ... 表示其它Unicode空白字符。
可以用下面的代码验证:
class Test
{
static void Main()
{
foreach (string s in " a bc d ".Split(' '))
System.Console.Write("[{0}]", s);
}
}
结果是: [][a][][bc][d][]
但我希望是: [a][bc][d]
自已写是不难,但这是很常用的功能,我想.NET framework应该支持的才对。
如果用自己写的,每次使用时都要加入自己写的函数,很麻烦。
class Test
{
static void Main()
{
string s=" a bc d ";
Regex rule=new Regex ("[a-z][^ ]*");
MatchCollection mc=rule.Matches (s);
foreach (Match m in mc)
{
System.Console.Write("[{0}]", m.Value);
}
}
}
foreach (string s in " a bc d ".Split(' ')) if(s!="")
System.Console.Write("[{0}]", s);另:不要称呼别人为"楼上的",那样很不礼貌.
s.Replace(" ",",")string [] ss = s.Split(new char[]{','});
不过,有一个失误(第二句)我的如下:
string s = " a bc d ";
s=s.Trim().Replace(" ","\n");
char[] chr=null;string [] ss = s.Split(n);不过,这样也是不行的,因为a和bc之间有两个空格
s.Replace(" ",",")
应改为: s = s.Replace(" ",",");
因为 string 是不可变的。
此外,这个还是产生:[][a][][bc][d][]
因为这只是把空格替换为",",然后用","作分隔符而已,没有实质性的改变。
把
string [] ss = s.Split(chr);
这句改一下
好象a和bc之间是没有两个空格的
是我多虑了
我是想取得空白分隔的字段的值。如有以下文件in.txt:
15 1234 7 8
3 5 8 9
32 -75 4 12
程序片段如下:
StreamReader sr = new StreamReader("in.txt");
for (;;)
{
string s = sr.ReadLine();
if (s == null) break;
string [] ss = s.Split();
int [] a = new int [ss.Length];
for (int i = 0; i < a.Length; i++)
{
a[i] = int.Parse(ss[i]);
}
// 处理 a[i] ...
}
sr.Close();
in.txt 文件中有四栏,我希望处理这四栏的值。
但 Split() 会返回多余的空字符串,不能满足要求。
a 和 bc 之间是有两个空格的。
而且可能是多个空格,或者是'\t'之类。
我的意思是说分割空白定界的字符串。多个空白字符当作一个看。
看这个很快就可以实现了
string s = " a bc d ".Trim();
string[] sr = new string[5];
int i = 0 ; //数组长度
while( true )
{
int intIndexOf = s.IndexOf( ' ');
if( intIndexOf > 0 )
{
sr[i] = s.Substring( 0 , intIndexOf );
s = s.Substring( intIndexOf,s.Length - intIndexOf );
s = s.Trim();
i++;
}
else
{
sr[i] = s;
break;
}
}for( int tmp = 0 ; tmp <= i ; tmp ++ )
System.Console.Write("[{0}]", sr[tmp]);
string s = " a bc d ";
s=s.Trim().Replace(五个空格,一个空格);
s=s.Replace(两个空格,一个空格);
s=s.Replace(两个空格,一个空格);
//这样最多处理了中间有8个空格的情况,要是空格超过8个,前面再加一句
//s=s.Trim().Replace(两个空格,一个空格); 可以处理到16个
//再split s
string [] ss = s.Split(一个空格);
string sIn = " a bc d ";
sIn = sIn.Trim();
Regex r = new Regex(@"\s+");
string[] sArr = r.Split(sIn);
//sArr = {"a","bc","d"}//对于同样适用string sIn = " a bc\td ";
string s = " a bc\t d ";
ArrayList aryLst=new ArrayList();
s=s.Trim().Replace(" ","\n");
char[] chr=null;string [] ss = s.Split(chr);for (int i=0;i<ss.Length;i++)
{
if (ss[i].Trim()!="")
aryLst.Add(ss[i]);
}aryLst就是所求
如果你非要string[]的话
加上ss=(string[])aryLst.ToArray(typeof(string));
string city;
city = city.Trim(new char[]{' ', ',', ';', '-', ':'});
你试试这个,简单的办法,可以去除很多不想要的字符,自己改吧。
string.split(..)方法
对于可能用多个空格分隔的话:
System.Text.RegularExpressions.Regex.Split(待分解串,格式串)的方法来分解,不需要创建一个Regex对象。
class Test
{
static void Main()
{
foreach (string s in " a bc d ".Split(' '))
{ if(s.Trim().lenght!=0)
System.Console.Write("[{0}]", s);
}
}
}
string [] ss = Regex.Split(s.Trim(), @"\s+");
// ss == {"a","bc","d"}