er ser
er ser

Reputation: 41

can't remove the reserved character?

I am trying to remove the reserved character but after calling my function it returns the same input..

        public static void Main(string[] args)
        {
            string text = "erhan?test*.pdf";             

            remove(text);

            Console.WriteLine("This is your result : {0}", text);
            Console.ReadLine();           

        }

         private static string remove(string str){

             StringBuilder sb = new StringBuilder();
             foreach (char c in str)
             {
                 if (Char.IsLetterOrDigit(c) || c == '.' || c == '_' || c == ' ' || c == '%')
                 { sb.Append(c); }
             }
             return sb.ToString();
        }

Upvotes: 0

Views: 51

Answers (2)

krivtom
krivtom

Reputation: 24916

In your code you are not updating the variable text (which to be honest you cannot do because string is immutable), but you are returning a new string from the function remove. Currently you are not using the returned value anywhere, so this value is effectively lost.

In order to make your code workyou need to assign your variable text to result of the function:

text = remove(text);

Upvotes: 3

Luke Vo
Luke Vo

Reputation: 20778

Chang this line

remove(text);

to

text = remove(text);

Because your function return a new string (built from StringBuilder), and you just call it without obtaining the result.

Upvotes: 3

Related Questions