Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
C#
string str = @":*?)";

            char[] invalidCharacters = Path.GetInvalidFileNameChars();

            for (;;)
            {
                int index = str.IndexOfAny(invalidCharacters);
                if (index < 0)
                    break;

                char c4 = str[index];
                string replace = string.Format(@"{0:x4}", (ushort)c4);
                str = str.Replace(c.ToString(), replace);
            }


The for loop is becoming infinite.
Posted

Depends - did you define the varaiable c outside the loop? Did you mean to use c4 instead?
C#
char c4 = str[index];
string replace = string.Format(@"{0:x4}", (ushort)c4);
str = str.Replace(c.ToString(), replace);
Becomes:
C#
char c4 = str[index];
string replace = string.Format(@"{0:x4}", (ushort)c4);
str = str.Replace(c4.ToString(), replace);
 
Share this answer
 
Comments
srikanth.enigma 29-Jan-13 6:17am    
sorry for wasting u r precious time.
OriginalGriff 29-Jan-13 6:22am    
You didn't - sometimes it takes a second pair of eyes to see the obvious - I do it all the time (I read what I wanted to write, not what I actually wrote :laugh:)
You have to make changes in the following line.
Instead of c use c4

C#
str = str.Replace(c4.ToString(), replace);
 
Share this answer
 
v2
Comments
srikanth.enigma 29-Jan-13 6:17am    
sorry for wasting u r precious time.
Hi Srikanth,
You should use regex Replace method to replace many instances at once.

See simple example:
C#
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is   text with   far  too   much   " +
                     "whitespace.";
      string pattern = "\\s+";
      string replacement = " ";
      Regex rgx = new Regex(pattern);
      string result = rgx.Replace(input, replacement);

      Console.WriteLine("Original String: {0}", input);
      Console.WriteLine("Replacement String: {0}", result);
   }
}
// The example displays the following output:
//       Original String: This is   text with   far  too   much   whitespace.
//       Replacement String: This is text with far too much whitespace.

Cheers,
Edo
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900