我怎么能用空格分割它。 (第一行是它的标题) [英] How can I split this by whitespaces. (the first lines is its header)

查看:95
本文介绍了我怎么能用空格分割它。 (第一行是它的标题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片



我试过这个代码但错误索引超出范围在cbay.ABS = columnsC [5],因为第二行只返回4而不是像第1行中的6个元素。我希望第二行也返回6个元素。



 使用( StringReader strrdr =  new  StringReader(strData))
{
string str;
while ((str = strrdr.ReadLine())!= null
{
// str = str.Trim();
if ((Regex.IsMatch(str.Substring( 0 1 ), @ J))||(Regex.IsMatch(str.Substring) ( 0 1 ), @ C)))
{
columnsC = Regex.Split(str, +);
cbay.AC = columnsC [ 1 ];
cbay.AU = columnsC [ 2 ];
cbay.SA = columnsC [ 3 ];
cbay.ABS = columnsC [ 5 ];
// cbay.ABS = str;
}
}
}

解决方案

首先,停止使用正则表达式 - 它们是一个用来破解一个非常简单的螺母的雪橇。

例如,这个:

  if ((Regex.IsMatch(str.Substring(  0  1 ), @ < span class =code-string> J))||(Regex.IsMatch(str.Substring( 0  1 ), @  C)))

可以这样重写:

  if (str [ 0 ] == '  J' || str [  0 ] == '  C'

这是一个更容易阅读的东西!



其次,拆分数据可能不是你想做的事情:因为它是按严格的列组织的,我会手动提取标题行,然后使用子字符串来提取数据元素:一些元素只有3组数字这一事实很重要,因为这个数字出现的列告诉你它是哪一部分的数据:在你的C行的情况下,数字是AU, SA和BSS值,而不是您在现在进行原油分割操作时必须从数据中假设的AC,AU和SA值。



使用string.SubString与string.Trim结合来分解你的行,然后使用int.TryParse给你实际的值而不是假设字符值。


非常感谢你。我改写了这样:



 使用(StringReader strrdr = < span class =code-keyword> new  StringReader(strData))
{
string str;
while ((str = strrdr.ReadLine())!= null
{
str = str.TrimStart();

if (str.Length > 20 &&(str [ 0 ] == ' C' || str [ 0 ] == ' 我'))
{

cbay.AC = str.Substring( 1 8 )。Trim();
cbay.AU = str.Substring( 9 5 )。Trim();
cbay.SA = str.Substring( 14 5 )。Trim();
cbay.ABS = str.Substring( 24 5 )。Trim();


}

if (str.Length > 20 && str [ 0 ] == ' Y'
{

cbay.AC1 = str.Substring( 1 8 )。Trim();
cbay.AU1 = str.Substring( 9 5 )。Trim();
cbay.SA1 = str.Substring( 14 5 )。Trim();
cbay.ABS1 = str.Substring( 24 5 )。Trim();

// 'Y'总是在'C'和'I'之后,所以打破
break ;
}

}


}
// return;
if String .IsNullOrEmpty (cbay.AC))cbay.AC = 0;
if String .IsNullOrEmpty(cbay.AU))cbay.AU = 0;
if string .IsNullOrEmpty(cbay.SA))cbay.SA = 0;
if String .IsNullOrEmpty(cbay.ABS))cbay.ABS = 0;

if String .IsNullOrEmpty(cbay.AC1))cbay.AC1 = 0;
if String .IsNullOrEmpty(cbay.AU1))cbay.AU1 = 0;
if string .IsNullOrEmpty(cbay.SA1))cbay.SA1 = 0;
if String .IsNullOrEmpty(cbay.ABS1))cbay.ABS1 = 0;

cbay.totalAU =( int .Parse(cbay.ABS)+ int .Parse(cbay.ABS1))的ToString();


Image

I have tried this code but error "index out of range" at cbay.ABS = columnsC[5] because the second line return only 4 instead of 6 elements like in 1st line. I want the 2nd line also return 6 elements.

using (StringReader strrdr = new StringReader(strData))
{
    string str;
    while ((str = strrdr.ReadLine()) != null)
    {
        //  str = str.Trim();
        if ((Regex.IsMatch(str.Substring(0, 1), @"J")) || (Regex.IsMatch(str.Substring(0, 1), @"C")))
        {
            columnsC = Regex.Split(str, " +");
            cbay.AC = columnsC[1];
            cbay.AU = columnsC[2];
            cbay.SA = columnsC[3];
            cbay.ABS = columnsC[5];
            // cbay.ABS = str;
        }
    }
}

解决方案

Firstly, stop using regexes - they are a sledghammer to crack a very simple nut.
For example, this:

if ((Regex.IsMatch(str.Substring(0, 1), @"J")) || (Regex.IsMatch(str.Substring(0, 1), @"C")))

Can be rewritten as this:

if (str[0] == 'J' || str[0] == 'C')

which is a heck of a lot easier to read!

Secondly, splitting your data is probably not what you want to do: since it is organised in strict columns, I would manuaglly extract the header line, then use substring to extract the data elements: the fact that some elements have only 3 sets of numbers is important, becasue teh column in which the number occurs tells you which part of the data it is: in teh case of your "C" line, the numbers are the AU, SA, and BSS values, not the AC, AU and SA values you would have to assume from the data if you do a crude split operation as you are doing at present.

Use string.SubString in combination with string.Trim to break up your line, then use int.TryParse to give you actual values instead of assuming character values.


thank you very much. I had rewriten like this:

using (StringReader strrdr = new StringReader(strData))
                       {
                           string str;
                           while ((str = strrdr.ReadLine()) != null)
                           {
                               str = str.TrimStart();

                               if (str.Length > 20 && (str[0] == 'C' || str[0] == 'I'))
                               {

                                   cbay.AC = str.Substring(1, 8).Trim();
                                   cbay.AU = str.Substring(9, 5).Trim();
                                   cbay.SA = str.Substring(14,5).Trim();
                                   cbay.ABS = str.Substring(24,5).Trim();


                               }

                               if (str.Length > 20 && str[0] == 'Y')
                               {

                                   cbay.AC1 = str.Substring(1, 8).Trim();
                                   cbay.AU1 = str.Substring(9, 5).Trim();
                                   cbay.SA1 = str.Substring(14, 5).Trim();
                                   cbay.ABS1 = str.Substring(24,5).Trim();

                                  //  'Y' always come after 'C' and 'I',  so break
                                   break;
                               }

                           }


                       }
                       //return;
                       if (String.IsNullOrEmpty(cbay.AC))  cbay.AC  = "0";
                       if (String.IsNullOrEmpty(cbay.AU))  cbay.AU  = "0";
                       if (string.IsNullOrEmpty(cbay.SA))  cbay.SA  = "0";
                       if (String.IsNullOrEmpty(cbay.ABS)) cbay.ABS = "0";

                       if (String.IsNullOrEmpty(cbay.AC1))  cbay.AC1  = "0";
                       if (String.IsNullOrEmpty(cbay.AU1))  cbay.AU1  = "0";
                       if (string.IsNullOrEmpty(cbay.SA1))  cbay.SA1  = "0";
                       if (String.IsNullOrEmpty(cbay.ABS1)) cbay.ABS1 = "0";

                       cbay.totalAU = ( int.Parse(cbay.ABS) + int.Parse(cbay.ABS1)).ToString();


这篇关于我怎么能用空格分割它。 (第一行是它的标题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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