将字符串作为数组访问,检查索引的值并可能更改该索引中的字符

时间:2013-04-18 22:19:27

标签: c# .net string

我一直在尝试读取一个字符串,然后逐个为每个字符运行一个随机数,如果生成一个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 个答案:

答案 0 :(得分: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());

答案 1 :(得分:1)

尝试使用StringBuilder

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

答案 2 :(得分: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());

答案 3 :(得分: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 ;
  }

}