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);.

4

4 回答 4

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();
}
于 2013-10-08T10:21:58.007 回答
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);
于 2013-10-08T10:23:38.010 回答
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.

于 2013-10-08T10:22:53.340 回答
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.

于 2013-10-08T10:31:23.997 回答