如何将字符串拆分为列 [英] How do I split string to column

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

问题描述

嗨朋友们,



我需要帮助将字符串拆分为数据栏



这意味着Hello World



你好|世界。



以下是我的代码,如果我执行下面的代码,它就会出现



Hello

世界



那么哪里和哪些我做错了?



Hi friends,

I need a help to split string to data column

which means "Hello World"

Hello | world.

below is my code, if i execute the below code, its coming out as

Hello
world

so where and what im doing wrong?

DataRow RowValues = database1DataSet.Table.NewRow();

      database1DataSet.Table.Columns.Add(dataColumn);
      string[] word = result.Text.Split(':');
      foreach (string item in word)
      {

          RowValues[1] = item;
        }





我尝试过:



我试过google但找不到答案



What I have tried:

I've tried google but can't find answer

推荐答案

嗯。您的代码无法满足各种原因:

1)您要拆分的字符与代码上方文本中的任何一个版本都不匹配。在那里你说它是
Um. Your code doesn't work for a whole load of reasons:
1) The character you are splitting on does not match either of the versions in the text above your code. There you say it is
"hello" space "world"

"hello" verical_bar "world"

但是你的代码寻找

"hello" colon "world"



:

string[] word = result.Text.Split(':');

请检查您的输入实际是什么,应该是什么,并修改您的代码以匹配!



2) RowValues [1] 是一个元素,它永远不会改变。因此,当您为其分配两次值时,第一个版本将被丢弃并被第二个版本覆盖。所以如果你在一个循环中使用它:

So check what your input actually is, and what it should be, and modify your code to match!

2) RowValues[1] is a single element, it never changes. So when you assign a value to it twice, the first version is thrown away and overwritten by the second. So if you use this in a loop:

RowValues[1] = item;

你将只获得最后一个值,而不是任何一个其他。可能你需要使用一个计数器来让你索引到每一列。



3)我很惊讶代码编译 - 我怀疑它没有。它通常不是 DataSet.Table 因为DataSet是DataTable对象的集合,所以它没有Table属性 - 它有一个Tables集合属性,你可以使用它可以通过索引访问以选择特定的表:

You will only ever get the last value, never any of the others. Probably, you need to use a counter to allow you to index into each column instead.

3) I'm surprised that code compiles - I suspect it doesn't. It would normally not be DataSet.Table because a DataSet is a collection of DataTable objects, so it doesn't have a Table property - it has a Tables collection property, which you can access via an index to select a specific table:

myDataSet.Tables[0]

例如。



4)为什么在为它构建新行后向表中添加一个新列 - 只有一个?这似乎是一种奇怪的做法...

For example.

4) Why are you adding a new column - and just one - to the table after you have built a new row for it? That seems an odd way to do things...


好我的原始数据是你好:世界,当我执行数据时应该进入



col1 col2

你好世界





而是显示



col1 col2

hello hellow

世界世界
Ok my raw data is hello:world, when i execute the data should go into

col1 col2
hello world


but instead its showing

col1 col2
hello hellow
world world


这篇关于如何将字符串拆分为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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