2015-12-22 29 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(); 
相關問題