2015-12-22 38 views
0

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

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

“如果”,只有当行包含的数量,当行不被执行新队。 else执行写入数组中的其他行(我使用console.writeLine查看结果,我知道我必须更改该部分) 执行此代码时,我在“如果”声明:

类型‘System.ArgumentOutOfRangeException’未处理的异常出现在mscorlib.dll

其他信息:索引和长度必须引用在字符串中的位置。

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

+0

你的意思是'0'和'1'是文本的一部分? – GolezTrol

+0

本来是不错的,如果你会提供一个可编译的代码,会产生你的问题...我无法重现它,但下面你会发现一个基于你的代码片段工作示例代码... – DarkSquirrel42

回答

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

这个代码,您递减lineIndex这是真的,但if(dotIndex <1)//必须至少位于2或以上的位置 可以省略写入不包含索引的行。结果就像我这样:0.ExampleFirst 1.ExampleSecond。所以我看不到其他行不包含lineIndex – mfo12002

+0

它们都在lineCounter中。然而,如果你想在循环中“看”它们,那么在继续之前做一些事情。我更新了代码供您参考。您也可以在整个循环完成后将它们全部打印出来,因为行在lineCounter数组中全部更新。 – Ian

+0

是的,那是真的。谢谢! ;) – mfo12002

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

我想要它喜欢这个:0。ExampleFirst \ n SomeText \ n SomeOtherText \ n FinalLine \ n \ n 1.ExampleSecond \ n SomeText \ n SomeOtherText \ n FinalLine \ n \ n – mfo12002

0

我相信你可能有与字符串数组上的空行的问题。 也许这样的东西适合你。

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();