繁体   English   中英

如何减少字符串数组中一行的索引?

[英]How to decrement the index of a line in an array of strings?

我正在编写减少字符串数组中的行索引的代码。 我的数组是这样的:

1.ExampleFirst\n
  SomeText\n
  SomeOtherText\n
  FinalLine\n\n

2.ExampleSecond\n
  SomeText\n
  SomeOtherText\n
  FinalLine\n\n

等等。 线的长度不相同。 我希望文字如下所示:

0.ExampleFirst\n
  SomeText\n
  SomeOtherText\n
  FinalLine\n\n

1.ExampleSecond\n
  SomeText\n
  SomeOtherText\n
  FinalLine\n\n

我做了这段代码:

int s = 0;

 while(s < lineCounter.Count())
   {
 if (int.TryParse(lineCounter[s].Substring(0, 1), out v) == true && lineCounter[s] != "\n") 
    {
      int m = int.Parse(lineCounter[s].Substring(0,1));
      lineCounter[s].Remove(0, lineCounter[s].IndexOf(".")).Insert(0, (m - 1).ToString());
      Console.WriteLine(lineCounter[s]);
     }
 else
       Console.WriteLine(lineCounter[s]);
       s++;

仅当该行包含数字并且该行不是新行时才执行“ if”。 执行else来将其他行写入数组中。(我正在使用console.writeLine查看结果。我知道我必须更改该部分。)执行此代码时,在“ ”声明:

mscorlib.dll中发生类型为'System.ArgumentOutOfRangeException'的未处理异常

附加信息:索引和长度必须引用字符串中的位置。

对我来说,这意味着即使遇到第一个文本块的最后一行和第二个文本块的第一行之间的新行,也会执行“ if”。 我无法解释原因。 请帮助!

使用string.Remove()string.Insert()组合声明dotIndex可能会成功。

string[] lineCounter = new string[]{
    "1.ExampleFirst\n",
    "  SomeText\n",
    "  SomeOtherText\n",
    "  FinalLine\n\n",
    "2.ExampleSecond\n",
    "  SomeText\n",
    "  SomeOtherText\n",
    "  FinalLine\n\n"
};

for (int i = 0; i < lineCounter.Count(); ++i) {
    int dotIndex = lineCounter[i].IndexOf('.');
    if (dotIndex < 1) //must be at least in the position of 2 or above
        continue;       
    int lineIndex = 0;
    if (int.TryParse(lineCounter[i].Substring(0, dotIndex), out lineIndex)) { //if can be parsed
        lineIndex--; //decrement lineIndex
        lineCounter[i] = lineIndex.ToString() + lineCounter[i].Remove(0, dotIndex);
    }
}

我更喜欢使用for-loop使循环更明确,但是您可以将其更改为while / do

在我的电脑上可以正常工作。 输出:

在此处输入图片说明

编辑:

所有结果应在lineCounter中。 如果要在功能中查看它们,可以执行以下操作:

for (int i = 0; i < lineCounter.Count(); ++i) {
    int dotIndex = lineCounter[i].IndexOf('.');
    if (dotIndex < 1) { //must be at least in the position of 2 or above
        //Print here
        continue;       
    }
    int lineIndex = 0;
    if (int.TryParse(lineCounter[i].Substring(0, dotIndex), out lineIndex)) { //if can be parsed
        lineIndex--; //decrement lineIndex
        lineCounter[i] = lineIndex.ToString() + lineCounter[i].Remove(0, dotIndex);
    }
    //Print here also
}

您可能想要这样:

string[] lineCounter=new string[]{
"1.ExampleFirst\n",
"  SomeText\n",
"  SomeOtherText\n",
"  FinalLine\n",
"\n",
"2.ExampleSecond\n",
"  SomeText\n",
"  SomeOtherText\n",
"  FinalLine\n",
"\n"
};
int v = 0;
int s = 0;

while (s < lineCounter.Count())
{
    if (int.TryParse(lineCounter[s].Substring(0, 1), out v) == true && lineCounter[s] != "\n")
    {
        int m = int.Parse(lineCounter[s].Substring(0, 1));
        Console.WriteLine(lineCounter[s].Remove(0, lineCounter[s].IndexOf(".")).Insert(0, (m - 1).ToString()));
    }
    else
        Console.WriteLine(lineCounter[s]);
    s++;
}

我相信您可能在字符串数组上出现空行问题。 也许这样的事情适合您。

IEnumerable<string> ShiftIndexes(IEnumerable<string> lines) {
    foreach (string line in lines) {
        if (!string.IsNullOrEmpty(line) && char.IsDigit(line, 0)) {
            int dotPos = line.IndexOf('.');
            int index = int.Parse(line.SubString(0, dotPos));
            yield return (index - 1).ToString() + line.SubString(dotPos);
        }
        else
            yield return line;
    }
}

然后像这样使用它:

string[] shiftedLines = ShiftIndexes(originalLines).ToArray();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM