Vb.net在字符串中选择数据 [英] Vb.net select data in string

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

问题描述

您好。因为我有问题。我想分离数据,例如以下示例。提前谢谢。





注意:^^^ X ^ A1 ..... ^ B1 ..... ^ C1 ..... ^ D1 ... ^ E1 .... ^ A2 .... ^ B2 ... ^ C2 .... ^ D2 ... ^ E2 ... ^^^ _ X

....(任何数据都可以)





样本字符串= ^^^ X ^ A12505361170 ^ B1255907002 ^ C1ABCD ^ D12555 ^ E12014 / 10/10 23:10:02 ^^^ _ X



Textbox1.text = 2505361170



textbox2.text = 255907002



textbox3.text = ABCD



textbox4.text = 2555



textbox5.text = 2014/10/10 23:10:02

解决方案

有几种方法可以做到这一点。



正则表达式是单向的,但我认为这种情况可能有点过于复杂。 />


您可以使用强力解析...即一次读取一个字符的字符串,忽略^和之后的任何内容,构建另一个字符串,直到您触及另一个字符串^。但这非常笨重。



或者你可以使用内置的VB.NET方法 Split() [ ^ ]



与此类似的东西可行:

  Dim 示例作为  String  =   ^^^ X ^ A12505361170 ^ B1255907002 ^ C1ABCD ^ D12555 ^ E12014 / 10/10 23:10:02 ^^^ _ X 

Dim splitSample 作为 字符串()=拆分(示例, ^, - 1,StringSplitOptions.RemoveEmptyEntries)

对于 每个 token As String splitSample
如果 token.Length> 1 然后
Debug.Print(token.Substring( 2 )) 这是您分配给文本框的位置
结束 如果
下一步


Hello. As I have a problem. I want to separate the data, such as in the following example. Thank you in advance.


note: ^^^X^A1.....^B1.....^C1.....^D1....^E1....^A2....^B2...^C2....^D2....^E2...^^^_X
....( Any data can be)


sample string= ^^^X^A12505361170^B1255907002^C1ABCD^D12555^E12014/10/10 23:10:02^^^_X

Textbox1.text=2505361170

textbox2.text=255907002

textbox3.text=ABCD

textbox4.text=2555

textbox5.text=2014/10/10 23:10:02

解决方案

There are several ways you could do this.

Regular Expressions are one way, but I think it might be a bit too complex for this scenario.

You could use "brute force" parsing ... i.e. read the string one character at a time, ignoring ^ and anything immediately afterwards, building up another string until you hit another ^. But that is very clunky.

Or you could use the built-in VB.NET method Split()[^]

Something similar to this would work:

Dim sample As String = "^^^X^A12505361170^B1255907002^C1ABCD^D12555^E12014/10/10 23:10:02^^^_X"

Dim splitSample As String() = Split(sample, "^", -1, StringSplitOptions.RemoveEmptyEntries)

For Each token As String In splitSample
    If token.Length > 1 Then
        Debug.Print(token.Substring(2)) 'this is where you assign to your text boxes
    End If
Next


这篇关于Vb.net在字符串中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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