How to find duplicate special character in C#

I was able to find a certain character in C#, the character is of this kind '~' in my string.contains method.  What I am trying to do is find if there are duplicates of that string like ~~ or ~~~.  It appears that when I try to run my program, it sees them all the same as in ~, ~~ and ~~~.  What I would like to do is be able to decipher between the difference with ~, ~~ and ~~~.  So how could I get my program to be able to be able to tell the difference.  I have the following code:

try
            {
                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                    {
                        string line = null;
                        while ((line = reader.ReadLine()) != null)
                        {                            

                            if (line.Contains("~"))
                            {
                                Regex RgCheck = new Regex("[^a-z0-9]");

                                bool regbool = true;
                                if (regbool == RgCheck.IsMatch("~"))
                                
                                {
                                    line = Regex.Replace(line, "~", "~" + System.Environment.NewLine);
                                }

 }
                            else if (line.Contains("~~"))
                            {                              

                                Regex RgCheck = new Regex("[^a-z0-9]");

                                bool regbool = true;

                                if (regbool == RgCheck.IsMatch("~~"))                                                              
                                {
                                    line = Regex.Replace(line, "~~", "~" + System.Environment.NewLine);
                                }
                            }

Open in new window

VBBRettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
If you're just wanting multiple ~ characters replaced with a single one followed by a newline, it should be as simple as this:

try
            {
                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                    {
                        string line = null;
                        while ((line = reader.ReadLine()) != null)
                        {                            

                                    line = Regex.Replace(line, "~+", "~" + System.Environment.NewLine);
                                  

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KwoofCommented:
it looks like the putpose is to change ~ (or multiple ~) into ~ followed by a new line

                        while ((line = reader.ReadLine()) != null)
                        {
                            while (line.Contains("~~"))
                            {
                                Regex RgCheck = new Regex("[^a-z0-9]");
                                if (RgCheck.IsMatch("~~"))
                                {
                                    line = Regex.Replace(line, "~~", "~");
                                }
                            }

                            if (line.Contains("~"))
                            {
                                Regex RgCheck = new Regex("[^a-z0-9]");
                                if (RgCheck.IsMatch("~"))
                                {
                                    line = Regex.Replace(line, "~", "~" + System.Environment.NewLine);
                                }

                            }
0
VBBRettAuthor Commented:
That's the thing, the only reason I would want to change anything is if there are two or three of the ~ characters in a row.  The issue that I am faced with is that on the same line, I could have more then one ~ with text in between, in that case, I would want the break line syntax to be added immediately after each ~.  Thanks!
0
VBBRettAuthor Commented:
and by the way, line.contains("~~") did not work for me.  It saw ~ and ~~ and ~~~ as just ~.  So if I had an if statement, it would default to the if with the one ~.
0
Terry WoodsIT GuruCommented:
Then just change the pattern to this to do a replace on 2 or more:

try
            {
                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                    {
                        string line = null;
                        while ((line = reader.ReadLine()) != null)
                        {                            

                                    line = Regex.Replace(line, "~{2,}", "~" + System.Environment.NewLine);
                                  
                                            

Open in new window


Presumably you won't have more than 3 in a row?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.