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

时间:2015-12-22 00:50:46

标签: c#

我正在制作一个代码,用于递减字符串数组中的行索引。我的阵列是这样的:

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来查看结果。我知道我必须更改那部分) 当我执行此代码时,我在“if”语句中得到以下异常:

  

mscorlib.dll中出现未处理的“System.ArgumentOutOfRangeException”类型异常

     

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

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

3 个答案:

答案 0 :(得分:1)

使用dotIndexstring.Remove()的组合声明string.Insert()可以解决问题。

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

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

enter image description here

编辑:

所有结果都应该在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
}

答案 1 :(得分:0)

你可能想要这个:

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++;
}

答案 2 :(得分:0)

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

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

然后像这样使用它:

import scalaz._
import Scalaz._

val et1:EitherT[Future, String, Int] = EitherT(Future.successful(1.right))

val et2:EitherT[Future, String, String] = EitherT(Future.successful("done".right))

val r:EitherT[Future, String, String] = for {
    a <- et1
    b <- et2
} yield (s"$a $b")