在 VB.NET 中是否有任何简单的方法来分割文本?

发布于 2024-11-18 05:30:50 字数 1264 浏览 1 评论 0原文

在 VB.NET 中是否有任何简单的方法来分割文本? (使用开始和结束字符串来获取中间的内容?)

我一直在 JScript 中使用以下命令执行此操作:

<前><代码><垃圾> <废话> <数据>someData1 <数据>someData2 <数据>someData3
var data = string.split('<data>')[1].split('</data>')[0];

通过将 [1] 索引更改为 [2] 会给我“someData1” 会很容易地给我“someData2”

由于某种原因,这似乎很难在 VB.NET 中实现。

这是我正在处理的实际 HTML 的一部分:

<...malformed html>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:48'>Total</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:346.2141'>18,072.59</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:444.3433'>100.00%</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:567.1293'>21,687.11</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:666.3433'>100.00%</div>
<malformed html...>

我需要找到

Total
。 index 然后获取第 1 个和第 3 个 div 之间的数据。

Is there any easy method for splitting text in VB.NET? (using a start and end string to grab whats in between?)

I do this all the time in JScript with the following:

<junk>
<blah>
<data>someData1</data>
<data>someData2</data>
<data>someData3</data>
</blah>
</junk>
var data = string.split('<data>')[1].split('</data>')[0];

would give me "someData1" by changing the [1] index to [2] would give me "someData2" very easy

for some reason this seems to be very difficult to achieve in VB.NET.

Here is a chunk of the actual HTML I'm dealing with:

<...malformed html>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:48'>Total</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:346.2141'>18,072.59</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:444.3433'>100.00%</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:567.1293'>21,687.11</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:666.3433'>100.00%</div>
<malformed html...>

I need to find the <div>Total</div> index then grab the data between the 1st and 3rd divs after that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来世叙缘 2024-11-25 05:30:50
Dim e = XElement.Parse(str)
Dim a = e.XPathSelectElements("./blah").Elements().ToArray()

a(0).Value 'someData1
a(1).Value 'someData2

编辑:
要解析 html,请尝试使用 Html Agility Pack

Dim e = XElement.Parse(str)
Dim a = e.XPathSelectElements("./blah").Elements().ToArray()

a(0).Value 'someData1
a(1).Value 'someData2

EDIT:
To parse html try using the Html Agility Pack

旧伤还要旧人安 2024-11-25 05:30:50

我让它工作了,尽管这是我写过的一些最糟糕的代码......

Dim sr As StreamReader

sr = New StreamReader("C:\test.html")

Dim xactHTML As String = sr.ReadToEnd

Dim left As Integer = xactHTML.IndexOf("Total</div>")
Dim chunk1 As String = xactHTML.Substring(left + 12)

Dim right As Integer = chunk1.IndexOf("<div style='position")
Dim chunk2 As String = chunk1.Substring(0, right - 1)

Dim xHTML As String = "<xml>" & chunk2 & "</xml>"

Dim e = XElement.Parse(xHTML)
Dim a = e.Elements().ToArray()

Dim damageAmmount As String = a(2).Value()

I got it working, although this is some of the worse code I've ever written...

Dim sr As StreamReader

sr = New StreamReader("C:\test.html")

Dim xactHTML As String = sr.ReadToEnd

Dim left As Integer = xactHTML.IndexOf("Total</div>")
Dim chunk1 As String = xactHTML.Substring(left + 12)

Dim right As Integer = chunk1.IndexOf("<div style='position")
Dim chunk2 As String = chunk1.Substring(0, right - 1)

Dim xHTML As String = "<xml>" & chunk2 & "</xml>"

Dim e = XElement.Parse(xHTML)
Dim a = e.Elements().ToArray()

Dim damageAmmount As String = a(2).Value()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文