there is a String which contains some @ characters, i want to find ” @ ” in my string and remove them, but it also finds and removes these ones: “@”
int atsignPlace = str.IndexOf(" @ ");
while (atsignPlace >= 0)
{
str = str.Remove(atsignPlace,3);
atsignPlace = str.IndexOf(" @ ");
}
i tried this code, but it removes nothing, so it always finds first ‘@’ ,which makes it an infinite loop.
int atsignPlace = str.IndexOf(" @");
while (atsignPlace >= 0)
{
if( atsignPlace+1 < str.Length && str[atsignPlace+1] == ' ' )
str = str.Remove(atsignPlace,3);
atsignPlace = str.IndexOf(" @ ");
}
Replace method also doesn’t work correct.
str = str.Replace(" @ ", String.Empty);
maybe there is a problem with ‘@’ character.
the input string is a sql query, i am trying to remove some parameters from it.
[ i have used try-catch for exceptions ]
Your code works fine. Short but complete program to demonstrate:
EDIT: Of course,
Replaceworks fine too:If you’re still seeing a problem with either of these, then the issue is in how you’re using this code, not in the code itself.
One guess: you might have non-printed characters within the ” @ ” which is preventing them from being removed. But you haven’t really given us enough information to say. A short but complete program demonstrating it not working would help…