如何在字符串中包含空格时按字符串拆分字符串 [英] How to split a string by some character when its having a space in string

查看:324
本文介绍了如何在字符串中包含空格时按字符串拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串说



  string  strDetail =  我的工作,123456,工程师,10000; 







如果我使用下面的代码分割字符串,我会得到例外。



< pre lang =c#> string []参数= new string [ 10 ];
参数= strDetail.Split(' ,');





当我显示Splitted字符串时,只显示My。



因为使用空格在我的和工作之间



但我希望输出为



我的工作
123456
工程师
10000



如何实现这一点。

解决方案

不,你没有。

再试一次:

  string  strDetail =   我的工作,123456,工程师,10000; 
string []参数= strDetail.Split(' );
foreach string s in 参数)
{
Console.WriteLine(s);
}





你会得到:

我的工作
123456
工程师
10000

相反,看看你的程序实际接收的是调试器中的strDetail字符串:你没有得到你所期望的,我想。


I am having a string say

string strDetail= "My Work,123456,Engineer,10000";




If I split the string using the code below I get exception.

string[] Parameters = new string[10];
Parameters=strDetail.Split(',');



When I display the Splitted string only "My" is shown.

Because a space is used between the words "My" and "Work"

But I want the output as

My Work
123456
Engineer
10000


How to achieve this.

解决方案

No, you don't.
Try it again:

string strDetail = "My Work,123456,Engineer,10000";
string[] Parameters = strDetail.Split(',');
foreach (string s in Parameters)
    {
    Console.WriteLine(s);
    }



And you will get:

My Work
123456
Engineer
10000

Instead, look at what your program is actually receiving as the strDetail string in the debugger: you aren't getting what you expect, I think.


这篇关于如何在字符串中包含空格时按字符串拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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