从字符串中获取文本 [英] Getting text from within a string

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

问题描述

我在使用VB .Net 2003.我正在将文件读入内存。这是文件的一个小部件


[88games

gamename ='''88 Game

numPlayers =

alternate =

mirrored =

tilt =

usesService =

miscDetails =这是一个4人团队类型的游戏,其中2个玩家每个时间竞争

P1NumButtons =

P1Controls = Just Buttons + butto

P1_BUTTON1 = Ru

P1_BUTTON2 = Jum

P1_BUTTON3 = Ru


[005

gamename = 00

numPlayers =

交替=

镜像=

tilt =

使用服务=

miscDetails

P1NumButtons =

P1Controls = 4向操纵杆+ joy4wa

P1_BUTTON1 = Fir

P1_JOYSTICK_UP = U

P1_JOYSTICK_DOWN = Dow

P1_JOYSTICK_LEFT = Lef

P1_JOYSTICK_RIGHT = Righ


[yard


它非常大。无论如何。现在我把它变成了一个变量,我想把一个特定的部分拉到另一个变量中。说a ?? [005] a ??部分。我有代码返回字符串a ?? [005] a ??开始但不知道从某个字符编号开始的命令或语法(我有),然后读取,直到第一个空行然后将其放入另一个变量


在那之后我想做同样的事情,那种,把部分放在a = = a ??之后在我的表格上签名单独的标签


我最好的办法是什么?


谢谢

Joh

解决方案

文章< 06 ***************** *****************@microsoft.co m> ;, jcrouse写道:

我使用VB .Net 2003.我正在读一个文件到记忆。这是文件的一个小节:

[88games]
gamename ='''88游戏
numPlayers = 2
alternating = 0
mirrored = 1
tilt = 0
usesService = 0
miscDetails =这是一个4人团队类型的游戏,每次有2名玩家竞争
P1NumButtons = 3
P1Controls = Just Buttons +按钮
P1_BUTTON1 =运行
P1_BUTTON2 =跳转
P1_BUTTON3 =运行

[005]
gamename = 005
numPlayers = 2 交替= 1
镜像= 1
倾斜= 0
usesService = 0
miscDetails =
P1NumButtons = 1
P1Controls = 4向操纵杆+ joy4way
P1_BUTTON1 = Fire
P1_JOYSTICK_UP = Up
P1_JOYSTICK_DOWN = Down
P1_JOYSTICK_LEFT = Left
P1_JOYSTICK_RIGHT =正确

[院子]

它非常大。无论如何。现在我把它变成了一个变量,我想把一个特定的部分拉到另一个变量中。说??[005]?部分。我有代码返回字符串的字符?[005]?开始但不知道从某个字符编号开始的命令或语法(我有),然后读取,直到第一个空行然后将其放入另一个变量。

之后我想要做同样的事,种类,把部分放在?=?之后在我的表格上签名单独的标签。

我最好的办法是什么?

谢谢你,
John




嗯,看起来像一个标准的Windows ini文件......就个人而言,我会建议使用P / Invoke来调用GetPrivateProfileString获取部分

你想要的。其他人可能不同意,但为什么要为系统已处理的某些东西编写解析例程。


私有声明自动函数GetPrivateProfileString Lib" kernel32" _

(ByVal lpAppName As String,_

ByVal lpKeyName As String,_

ByVal lpDefault As String,_

ByVal lpReturnedString As System.Text.StringBuilder,_

ByVal nSize As Integer,_

ByVal lpFileName As String)As Integer


通过将Nothing

传递给lpKeyName参数,您可以将整个部分读入单个缓冲区。它将用所有键填充缓冲区

由空字符分隔的部分...


你还有其他选择是查看系统。 Text.RegularExpressions to

从字符串中提取你想要的数据...


-

Tom Shelton [MVP]


jcrouse写道:

我正在使用VB .Net 2003.我正在将文件读入内存。这是文件的小部件:
<省略>

它非常大。无论如何。现在我把它变成了一个变量,我想把一个特定的部分拉到另一个变量中。说[005]部分。我有代码返回什么字符
字符串[005]"开始但不知道从某个字符编号开始的命令或语法(我有),然后读取
直到第一个空行然后将其放入另一个变量。


你可能不想在内存中这样做,特别是因为你说

文件非常大。

为什么不使用StreamReader.ReadLine逐行读取文件,并且

在你去的时候解析文件。只需继续读取行并丢弃它们直到

你得到的行是[005],然后在解析后读取行

它们直到你到达一个空行。

之后我想做同样的事情,并且在=之后放置部分
在我的表单上签名单独的标签。




你想要使用String.Split。


-

Sven Groot

http: //unforgiven.bloghorn.com


这一切都很好,但我需要代码。如前所述,除了匹配表达式开头的起始字符数,我似乎无法存储或检索任何内容。我如何做一个StreamReader.ReadLine,然后将它放入一个变量或label.text属性


谢谢

John

Ia??m using VB .Net 2003. I am reading a file into memory. Here is a sniplet of the file

[88games
gamename=''88 Game
numPlayers=
alternating=
mirrored=
tilt=
usesService=
miscDetails=This is a 4 player team type game where 2 players compete each tim
P1NumButtons=
P1Controls=Just Buttons+butto
P1_BUTTON1=Ru
P1_BUTTON2=Jum
P1_BUTTON3=Ru

[005
gamename=00
numPlayers=
alternating=
mirrored=
tilt=
usesService=
miscDetails
P1NumButtons=
P1Controls=4-way Joystick+joy4wa
P1_BUTTON1=Fir
P1_JOYSTICK_UP=U
P1_JOYSTICK_DOWN=Dow
P1_JOYSTICK_LEFT=Lef
P1_JOYSTICK_RIGHT=Righ

[yard

It is quite large. Anyways. Now that I have it into a variable I would like to pull out a specific section into another variable. Say the a??[005]a?? section. I have the code returning what character the string a??[005]a?? starts at but dona??t know the command or syntax to start at a certain character number (which I have) and then read until the first blank line then put it into another variable

After that I want to do the same thing, kind of, and put the parts after the a??=a?? sign into separate labels that I have on my form

What is my best way to accomplish this

Thank you
Joh

解决方案

In article <06**********************************@microsoft.co m>, jcrouse wrote:

I?m using VB .Net 2003. I am reading a file into memory. Here is a sniplet of the file:

[88games]
gamename=''88 Games
numPlayers=2
alternating=0
mirrored=1
tilt=0
usesService=0
miscDetails=This is a 4 player team type game where 2 players compete each time
P1NumButtons=3
P1Controls=Just Buttons+button
P1_BUTTON1=Run
P1_BUTTON2=Jump
P1_BUTTON3=Run

[005]
gamename=005
numPlayers=2
alternating=1
mirrored=1
tilt=0
usesService=0
miscDetails=
P1NumButtons=1
P1Controls=4-way Joystick+joy4way
P1_BUTTON1=Fire
P1_JOYSTICK_UP=Up
P1_JOYSTICK_DOWN=Down
P1_JOYSTICK_LEFT=Left
P1_JOYSTICK_RIGHT=Right

[yard]

It is quite large. Anyways. Now that I have it into a variable I would like to pull out a specific section into another variable. Say the ?[005]? section. I have the code returning what character the string ?[005]? starts at but don?t know the command or syntax to start at a certain character number (which I have) and then read until the first blank line then put it into another variable.

After that I want to do the same thing, kind of, and put the parts after the ?=? sign into separate labels that I have on my form.

What is my best way to accomplish this?

Thank you,
John



Hmm, looks like a standard windows ini file... Personally, I would
suggest using P/Invoke to call GetPrivateProfileString to get the pieces
you want. Others may disagree, but why write a parsing routine for
something the system already handles:)

Private Declare Auto Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

You can read an entire section into a single buffer by passing Nothing
to the lpKeyName parameter. It will fill the buffer with all keys in
the section separated by null chars...

You''re other option is to look into System.Text.RegularExpressions to
extract the piece of data you would like from the string...

--
Tom Shelton [MVP]


jcrouse wrote:

I''m using VB .Net 2003. I am reading a file into memory. Here is a
sniplet of the file:
<omitted>

It is quite large. Anyways. Now that I have it into a variable I
would like to pull out a specific section into another variable. Say
the "[005]" section. I have the code returning what character the
string "[005]" starts at but don''t know the command or syntax to
start at a certain character number (which I have) and then read
until the first blank line then put it into another variable.
You probably don''t want to be doing this in memory, especially since you say
the file is quite large.
Why not instead read the file line by line using StreamReader.ReadLine, and
parse the file as you go. Just keep reading lines and discarding them until
you get the line that reads "[005]", then read the lines after that parsing
them until you get to a blank line.
After that I want to do the same thing, kind of, and put the parts
after the "=" sign into separate labels that I have on my form.



You''d want to use String.Split for that.

--
Sven Groot

http://unforgiven.bloghorn.com


That''s all well and dandy but I need code. As earlier stated, I can''t seem to store or retrieve anything but the starting character numer of the beginning of the matching expression. How do I do a StreamReader.ReadLine and then put it into a varaible or label.text property

Thank you
John


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

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