它将一个字符串分割 [英] splitting a string

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

问题描述

我有以下字符串:

http://pastebin.com/d29ae565b

我需要分隔每个值,并把它放在一个阵列。通常这将通过.split来完成。但是我需要它在这个顺序进行分割:0,50,100,200,400等。

i need to separate each value and put it in an array. typically it would be done by ".split". however i need it to be split in this order: 0, 50, 100, 200, 400 etc..

没有人知道如何做到这一点?请注意,我必须这样做在VB

does anyone know how to do this? please note that i need to do this in VB

换句话说,我需要它来读取行左到右。我没有问题,分隔每个数字,我只需要它在指定的顺序读取。

in other words, i need it to read the rows left to right. i have no problem separating each number, i just need it to read it in the specified order.

感谢大家的建议。香港专业教育学院尝试了正则表达式,我忘了提,每行后有一个换行符。我不知道这是否会影响到正则表达式,但在任何情况下,后我做的正则表达式,我得到以下顺序:0,6.65,84 ??,35 ....等

thank you all for your suggestions. ive tried the regexp and i forgot to mention that after each line there is a line break. i am not sure if this would impact the regex, but in any case, after i do the regex, i get the following order: 0, 6.65, 84..??, 35.... etc

我没有得到我需要上述

预期的结果:0,50,100,100,200,400,218,9.8,...,6.65,6.31等...

expected results: 0, 50, 100, 100, 200, 400, 218, 9.8, ???, 6.65, 6.31 etc...

我要去通过最初分手的字符串分成几行遵循一些建议如下。这code几乎这是否:

i am going to follow some of the suggestions below by splitting up the string into separate lines initially. this code almost does that:

Dim fields() As String
        fields = calculationText.Split(vbCrLf)

不幸的是由于某些原因的空间都将丢失。当我看着阵,所有的数字之间的空间都将丢失。为什么?????????

unfortunately for some reason the spaces are lost. when i look into the array, all the spaces between numbers are lost. why?????????

推荐答案

它像你需要两次分裂的事情听起来我。逐行读取文件中的行到一个数组,或者更好的一个列表(串),然后通过列表中的每个线进行迭代,并以此为基础进行的空间分割以后

It sounds to me like you need to split things twice. Read the file line by line into an array, or better yet a List(Of String), and then iterate through each "line" in the List and do a subsequent split based on the space.

在你做的每一行,你可以第一个元素添加到您的结果数组列表。

As you go through each line, you can add the first element into your result array, list.

编辑:由于您遇到一些麻烦,试试这个code,看看它是否为你的作品:

Since you're having some troubles, try out this code and see if it works for you:

Dim lstResulst As New List(Of String)
Dim lstContent As New List(Of String)
Dim LineItems() As String
Dim objStreamReader as StreamReader

objStreamReader = File.OpenText(FILENAME)

While objStreamReader.Peek() <> -1
  lstContent.Add(objStreamReader.ReadLine())
End While

objStreamReader.Close()

这所有读取每行文件行成一个列表(字符串)。然后从那里,你可以这样做:

This reads all of your files line per line into a List(Of String). Then from there you can do this:

For Each CurrLine As String In lstContent
   LineItems = CurrLine.Split(Char.Parse(" "))
   lstResults.Add(LineItems(0))
Next

这将每个项目分为数组,你可以转储分割的第一个项目进入一个新的列表(字符串)。可以很容易地倾倒到这一小数或任何的列表,并简单地缠绕在适当的转换方法的CurrLine.Split。上线的项目的其余部分将可用了LineItem数组中也是如此。

That'll split each item into an array and you can dump the first item of the split into a new List(Of String). You can easily dump this into a list of Decimals or whatever and simply wrap the CurrLine.Split around the appropriate conversion method. The rest of the items on the line will be available in the LineItems array as well.

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

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