2013-04-18 17 views
3

我一直在嘗試讀取一個字符串,然後逐個爲每個字符運行一個隨機數,如果生成了5將字符從1更改爲0,反之亦然。我覺得我差不多在這裏,這是我的第三次嘗試,但是我在寫入索引時遇到了一些小問題,因爲它告訴我它是隻讀的。這裏是我的代碼:作爲一個數組訪問一個字符串,檢查一個索引的值,並可能改變該索引中的字符

string mutate = "1010"; 

for (int i = 0; i < 4; i++) 
{ 
    int MutProbablity = random.Next(1, 1000); 
    if (MutProbablity == 5) 
    { 
     if (mutate[i] == '0') 
     { 
      mutate[i] = '1'; 
     } 
     else if (mutate[i] == '1') 
     { 
      mutate[i] = '0'; 
     } 
    } 
} 

這是我的錯誤:

Property or indexer ' string.this[int] ' cannot be assigned to -- it is read only

誰能告訴我怎樣才能解決這個問題,或者是提出一個不同的方法,我可以實現我的目標是什麼?

回答

10

.NET中的字符串是不可變的,因此您將無法修改任何字符,但是您可以通過首先將其轉換爲char[]來實現您的目標,這是可變的。

像這樣:

var chars = mutate.ToCharArray();  // convert to char[] 
for (int i = 0; i < 4; i++) 
{ 
    int MutProbablity = random.Next(1, 1000); 
    if (MutProbablity == 5) 
    { 
     if (chars[i] == '0') 
     { 
      chars[i] = '1'; 
     } 
     else if (chars[i] == '1') 
     { 
      chars[i] = '0'; 
     } 
    } 
} 

mutate = new String(chars); // convert to back to string 

另一種方式來做到這一點是使用LINQ的(和三元運營商的公認非常醜陋的字符串)。

var new string(mutate.Select(c => 
    (random.Next(1, 1000) == 5) ? ((c == '0') ? '1' : (c == '1') ? '0' : c) : c).ToArray()); 
+0

謝謝你,我現在要試試這個,我確信我想類似這樣的東西,但我一定是做錯了!我會讓你知道我怎麼樣! :) – deucalion0

+0

第二種方法看起來很混亂! :)我從來沒有使用Linq,但我聽說過它。乾杯! – deucalion0

+2

@ deucalion0這只是一個更緊湊的做同樣的事情。我實際上不會推薦它用於生產代碼,因爲它很難閱讀,但我只是爲了完整性而想包括它。 –

1

嘗試用StringBuilder

它提供了單個字符的變化,以及更多有用的方法

1

是您的mutate字符串只是由0和1?
StringBuilder的可以用來改變字符串

Random r = new Random(); 
StringBuilder b = new StringBuilder("0101"); 
for (int i = 0; i < 4; i++) 
{ 
    int MutProbablity = r.Next(1, 1000); 
    if (MutProbablity == 5) 
    { 
     b[i] = (b[i] == '0' ? '1' : '0'); 
    } 
} 
Console.WriteLine(b.ToString()); 
1

如前所述,字符串是不可變內單個字符,但是類StringBuilder確實是一個可變的字符串。使用一個StringBuilder作爲後備存儲,這些方針的東西創建一個自定義類:

class MyMutableString 
{ 
    private StringBuilder backingStore ; 
    private string  currentValue ; 
    public MyMutableString() 
    { 
    this.backingStore = new StringBuilder() ; 
    this.currentValue = this.backingStore.ToString() ; 
    return ; 
    } 
    public MyMutableString(string initialValue) 
    { 
    this.backingStore = new StringBuilder(initialValue) ; 
    this.currentValue = this.backingStore.ToString() ; 
    } 

    public string Value 
    { 
    get 
    { 
     return this.currentValue ; 
    } 
    set 
    { 
     this.backingStore.Length = 0 ; 
     this.backingStore.Append(value) ; 
     this.currentValue = this.backingStore.ToString() ; 
    } 
    } 

    public void Mutate(int mutationThreshold , Dictionary<char,char> mutations) 
    { 
    int probability = GetNextRandomValue() ; 
    if (probability == mutationThreshold) 
    { 
     int replacementsMade = 0 ; 
     for (int i = 0 ; i < this.backingStore.Length ; ++i) 
     { 
     char c = this.backingStore[i] ; 
     char r ; 
     bool mutate = mutations.TryGetValue(c, out r) ; 
     if (mutate) 
     { 
      this.backingStore[i] = r ; 
      ++replacementsMade ; 
     } 
     } 
     this.currentValue = this.backingStore.ToString() ; 
    } 
    return ; 
    } 

    private static readonly Random random = new Random() ; 
    private int GetNextRandomValue() 
    { 
    int value = random.Next(1,1000) ; 
    return value ; 
    } 

} 
+0

謝謝你的回答我將把這段代碼保存在我的工具箱中! :) 非常感激! – deucalion0

相關問題