1
using System;

namespace stringdouble
{
    public class populate
    {
        public string string1;
        public string string2;

        public populate (string string1 ,string string2)
        {
            for (int i=1; i < string1.Length; i++)
            {
                if (string1[i] == string1[i-1])
                {
                    string2[i] = string1[i];//error on this line
                    Console.WriteLine ("Character Copied");
                    Console.ReadKey();
                    Console.Clear();
                }
            }

            Console.WriteLine (string2);
        }
    }

This is the code but for some reason its giving me the error I entered above. I am trying to remove the duplicates in a string and copy them into a new one.

I call this by new populate(FirstString, SecondString);.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
user2858107
  • 53
  • 1
  • 6

4 Answers4

7

Strings are immutable, so once they are created you cannot modify them without creating a new string. That's why you get the exception when you try to modify a charcter from a string.

You could use Linq to get only unique characters:

string uniqueCharacters = new string(string1.Distinct().ToArray());

or, if you want all duplicate chars in a new string:

var dupChars = string1.GroupBy(c => c)
   .Where(g => g.Count() > 1)
   .Select(g => g.First());
string dupliateCharacters = new string(dupChars.ToArray());

As BartozKP has mentioned, maybe you want to create a string of equal adjacent characters. Here is a possible implementation using a StringBuilder and a loop:

public string Populate(string text)
{
    StringBuilder sb = new StringBuilder();
    if(text.Length > 1)
    {
        for (int i = 1; i < text.Length; i++)
        { 
            Char c = text[i];
            if (c == text[i - 1])
                sb.Append(c);
        }
    }
    return sb.ToString();
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
4

You may convert your string into an array of characters using String#ToCharArray(). In your code:

char[] chars1 = string1.ToCharArray();
....
string result = new string(chars);
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Using a `StringBuilder` also gives the ability to set separate `char` values by index, as in `myStringBuilder[7] = 'y';`, but it has lots of other nice string features as well, so he might prefer it over a simple `char[]` array. The array has many limitations, one being that it has a fixed length. – Jeppe Stig Nielsen Oct 08 '13 at 10:44
2

string is an alias for String in the .NET Framework. And .NET String is immutable. Make string2 an object of StringBuilder class to make your code work.

Sadique
  • 22,572
  • 7
  • 65
  • 91
0

A working version which doesn't your actual functionality:

public class populate
{
    public populate (string string1 ,string string2)
    {
        char[] result = string2.ToCharArray();

        for (int i=1; i < string1.Length; i++)
        {
            if (string1[i] == string1[i-1])
            {
                result[i] = string1[i];
                Console.WriteLine ("Character Copied");
                Console.ReadKey();
                Console.Clear();
            }
        }

        string2 = new string(result);

        Console.WriteLine (string2);
    }

Based on answer provided by HimBromBeere. However this won't remove all duplicates from the string1. If you really want to do this, refer to TimSchmelter's answer.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130