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

这是我的错误:

string.this[int]无法将属性或索引器“ ”分配给 -- 它是只读的

有人可以告诉我如何解决这个问题,或者建议一种不同的方式来实现我的目标吗?

4

4 回答 4

10

.NET 中的字符串是不可变的,因此您将无法修改任何字符,但您可以通过首先将其转换char[]为可变的 a 来实现您的目标。

像这样:

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());
于 2013-04-18T22:21:25.900 回答
1

尝试使用StringBuilder

它提供了单个字符更改,以及更多有用的方法

于 2013-04-18T22:24:40.557 回答
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());
于 2013-04-18T22:31:02.017 回答
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 ;
  }

}
于 2013-04-18T22:43:50.797 回答