Reputation: 41
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
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
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