So my program task is to load a text file that contains for example:
black | white
And then i open one another text that contain a lot of words, and if for example it will find the word black, it will replace it with white. This code below do that. My problem is that if the text contain Black (with first letter uppercase) it not replace it with White. I know i didn't write anything in the code that will do that but I dont know how to do it. Can someone fill my code?

private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd1 = new OpenFileDialog();
            ofd1.Filter = "Text Files|*.txt|SubRipText|*.srt";

            if (ofd1.ShowDialog() != DialogResult.Cancel)
            {
                string text = File.ReadAllText(ofd1.FileName);

                for (int x = 0; x < i; x++)
                {
                    if (text.Contains(arr[x, 0]))
                    {
                        text = text.Replace(arr[x, 0], arr[x, 1]);
                    }
                }
                File.WriteAllText(ofd1.FileName, text);
            }
        }

Recommended Answers

All 6 Replies

You can use Regex.Replace with the case insensitive option (and use Regex.Escape on the parameters, just in case). If you want to preserve the case of the found item then you'll have to do that on your own, and figure out how you're going to deal with situations where it's not possible to preserve case.

For example: black | white
Black would become White (case preserved)

But what do you do when you have: black | light aqua
Black would become Light aqua or Light Aqua?

How about: black | red
BlAcK would become?

But what do you do when you have: black | light aqua
Black would become Light aqua or Light Aqua?

It will become Light aqua

How about: black | red
BlAcK would become?

I will not have such problems, and even if I had I will do it manually no problem...

I just want if find Black replace it with White

please someone?

compare with all one case.... so text.ToUpper(). Then just do two replace statements, one for the upper case version, one for the lower case version. Otherwise you can google regular expressions and figure out the right format to use.

Compare both texts to "ToLower":

if (text.Contains(arr[x, 0].ToLower()))
{
    text = text.Replace(arr[x, 0], arr[x, 1]);
}

Your ideas helped me to though of this one... If someone interested here is the code

I made a function to set the first letter to uppercase

string FirstLetterToUpperCase(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            char[] a = s.ToCharArray();
            a[0] = char.ToUpper(a[0]);
            return new string(a);
        }

Then i saved the new string to temp and used this temp file

temp = FirstLetterToUpperCase(arr[x, 0]);
                    if (text.Contains(temp))
                    {
                        text = text.Replace(temp, arr[x, 1]);
                    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.