解析字符串 [英] Parse-ing Strings

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

问题描述




我收到一个从网页表单传回的字符串格式为

name1:variable1; name2:variable2 ...


我想把它分成几个变量,这样变量

名称是name1,name2等(我实际上知道这些名字是什么)并且变量的值是变量1,变量2等。


我尝试使用split将字符串拆分为分号中的数组,并且

然后认为我会用第二个拆分命令将值拆分出来,

但这似乎是解决问题的一个复杂方法。另外我有一些实施的问题。


我的两个问题是:

1.有什么问题代码原样

2.有什么更好的方法可以实现预期的效果


我的VB.NET代码背后是:

''声明变量

Dim DataString As String

Dim DataArray(9)As String

''从http <获取数据br />
DataString = Request(" DataString")

''解析数据

DataArray = TimeEntryData.Split(CChar(";") )''这是给出

错误的行

name1 = TimeEntryDataArray(1)

Response.Write(name1)


我得到的错误是:

对象引用未设置为对象的实例。


谢谢,

Martin

Hi,

I am getting a string passed back from a web form in the format
name1:variable1;name2:variable2...

I would like to split this up into several variable, such that the variable
names are name1, name2 etc. (I actually know what these names are) and the
values of the variables are variable1, variable2 etc.

I tried using split to split the string into an array at the semicolons, and
was then thinking I would split the value out with a second split command,
but this seems a bit of a complex way round the problem. Also I am having
some problems with the implementation.

My two questions are:
1. What is wrong with the code as it stands
2. What better way is there of acheiving the desired effect

My VB.NET code behind is something like:
''Declare Variables
Dim DataString As String
Dim DataArray(9) As String
''Get Data from http
DataString = Request("DataString")
''Parse Data
DataArray = TimeEntryData.Split(CChar(";")) ''This is the line that gives an
error
name1=TimeEntryDataArray(1)
Response.Write(name1)

And the Error I get is:
Object reference not set to an instance of an object.

Thanks,
Martin

推荐答案

在你的代码中,什么是TimeEntryData?

你似乎想要分裂 ' DataString

你应该有DataArray中= DataString.Split(CCHAR(英寸;"))


"马丁Eyles" <毫安********** @ NOSPAMbytronic.com>在消息中写道

新闻:11 ************* @ corp.supernews.com ...
In your code, what is TimeEntryData?
You seem to want to split ''DataString''
You should have DataArray = DataString.Split( CChar(";"))

"Martin Eyles" <ma**********@NOSPAMbytronic.com> wrote in message
news:11*************@corp.supernews.com...


我收到一个从web表单传回的字符串格式为
name1:variable1; name2:variable2 ...

我想把它拆分成几个变量,这样的变量名称是name1,name2等(我实际上知道这些名称是什么),变量的值是variable1,variable2等。

我尝试使用split将字符串拆分为分号中的数组,
然后我想我会用第二个拆分
命令将值拆分出来,但这似乎是解决问题的一个复杂方法。另外我对实现有一些问题。

我的两个问题是:
1.代码有什么问题
2.有什么更好的实现预期效果的方式

我的VB.NET代码背后是:
''声明变量
Dim DataString As String
Dim DataArray(9 )作为字符串
''从http获取数据
DataString = Request(" DataString")
''解析数据
DataArray = TimeEntryData.Split(CChar(" ;;) ;))''这是给出错误的行
name1 = TimeEntryDataArray(1)
Response.Write(name1)

我得到的错误是:
对象引用未设置为对象的实例。

谢谢,
Martin
Hi,

I am getting a string passed back from a web form in the format
name1:variable1;name2:variable2...

I would like to split this up into several variable, such that the
variable names are name1, name2 etc. (I actually know what these names
are) and the values of the variables are variable1, variable2 etc.

I tried using split to split the string into an array at the semicolons,
and was then thinking I would split the value out with a second split
command, but this seems a bit of a complex way round the problem. Also I
am having some problems with the implementation.

My two questions are:
1. What is wrong with the code as it stands
2. What better way is there of acheiving the desired effect

My VB.NET code behind is something like:
''Declare Variables
Dim DataString As String
Dim DataArray(9) As String
''Get Data from http
DataString = Request("DataString")
''Parse Data
DataArray = TimeEntryData.Split(CChar(";")) ''This is the line that gives
an error
name1=TimeEntryDataArray(1)
Response.Write(name1)

And the Error I get is:
Object reference not set to an instance of an object.

Thanks,
Martin





" Phillip N Rounds" < PR ***** @ cassandragroup.com>在消息中写道

news:ui ************** @ TK2MSFTNGP10.phx.gbl ...

"Phillip N Rounds" <pr*****@cassandragroup.com> wrote in message
news:ui**************@TK2MSFTNGP10.phx.gbl...
在你的代码中,什么是TimeEntryData吗?
你似乎想分割''DataString''
你应该有DataArray = DataString.Split(CChar(" ;;))
In your code, what is TimeEntryData?
You seem to want to split ''DataString''
You should have DataArray = DataString.Split( CChar(";"))




对不起,没有完成为一般情况重命名变量。代码

应为:

''声明变量

Dim DataString As String

Dim DataArray(9)作为字符串

''从http获取数据

DataString =请求(DataString)

''解析数据

DataArray = DataString.Split(CChar(";"))''这是给出

错误的行

name1 = DataArray(1)

Response.Write(name1)



Sorry, didn''t finish renaming the variables for general-ness. The code
should read:
''Declare Variables
Dim DataString As String
Dim DataArray(9) As String
''Get Data from http
DataString = Request("DataString")
''Parse Data
DataArray = DataString.Split(CChar(";")) ''This is the line that gives an
error
name1=DataArray(1)
Response.Write(name1)


Martin,

我认为使用split函数是获得价值的好方法。我不是意识到更好的方式。但也许其他人可能就此问候。


这里说的是一个小例子:


Dim TestData As String =" ; ;;;;;;;;;;;;;;;;;;;; ;))


作为字符串的暗淡值


每个测试作为数据中的字符串


价值=测试


下一页


请注意,我没有预先定义数组。

- -

此致,


S. Justin Gengo,MCP

网页开发人员/程序员

< a rel =nofollowhref =http://www.aboutfortunate.comtarget =_ blank> www.aboutfortunate.com


" out of混乱来了。

Nietzsche

" Martin Eyles" <毫安********** @ NOSPAMbytronic.com>在消息中写道

新闻:11 ************* @ corp.supernews.com ...
Martin,

I think using the split function is a fine way to get your values. I''m not
aware of a better way. But perhaps someone else may chime in here on that.

That said here''s a small example:

Dim TestData As String = "9;8;7;6;5;4;3;2;1;0"

Dim Data() As String = TestData.Split(CChar(";"))

Dim Value As String

For Each Test As String In Data

Value = Test

Next

Note that I''m not pre-defining the array.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Martin Eyles" <ma**********@NOSPAMbytronic.com> wrote in message
news:11*************@corp.supernews.com...


我收到一个从web表单传回的字符串格式为
name1:variable1; name2:variable2 ...

我想把它拆分成几个变量,这样的变量名称是name1,name2等(我实际上知道这些名称是什么),变量的值是variable1,variable2等。

我尝试使用split将字符串拆分为分号中的数组,
然后我想我会用第二个拆分
命令将值拆分出来,但这似乎是解决问题的一个复杂方法。另外我对实现有一些问题。

我的两个问题是:
1.代码有什么问题
2.有什么更好的实现预期效果的方式

我的VB.NET代码背后是:
''声明变量
Dim DataString As String
Dim DataArray(9 )作为字符串
''从http获取数据
DataString = Request(" DataString")
''解析数据
DataArray = TimeEntryData.Split(CChar(" ;;) ;))''这是给出错误的行
name1 = TimeEntryDataArray(1)
Response.Write(name1)

我得到的错误是:
对象引用未设置为对象的实例。

谢谢,
Martin
Hi,

I am getting a string passed back from a web form in the format
name1:variable1;name2:variable2...

I would like to split this up into several variable, such that the
variable names are name1, name2 etc. (I actually know what these names
are) and the values of the variables are variable1, variable2 etc.

I tried using split to split the string into an array at the semicolons,
and was then thinking I would split the value out with a second split
command, but this seems a bit of a complex way round the problem. Also I
am having some problems with the implementation.

My two questions are:
1. What is wrong with the code as it stands
2. What better way is there of acheiving the desired effect

My VB.NET code behind is something like:
''Declare Variables
Dim DataString As String
Dim DataArray(9) As String
''Get Data from http
DataString = Request("DataString")
''Parse Data
DataArray = TimeEntryData.Split(CChar(";")) ''This is the line that gives
an error
name1=TimeEntryDataArray(1)
Response.Write(name1)

And the Error I get is:
Object reference not set to an instance of an object.

Thanks,
Martin



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

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