Link to home
Start Free TrialLog in
Avatar of shieldguy
shieldguyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help needed to remove special character from the string

I need to parse a string with and remove all the following special characters like these

[

^
%
Ã
¨
¬
"
â

©

also the following
\t ---> (Tab)
\n ---> (new line)
\r  --->(carrige return)
I also like to to remove any single quote (')
Best practice is to remove all the special characters but my string data also have some html
tags and I dont want to remove them
e.g.
<ul> <li> files test </li></ul>

Thanks
Avatar of abel
abel
Flag of Netherlands image

What about a regular expression? This will do the trick (place all your special characters between the square brackets:

string cleanString = Regex.Replace(dirtyString, @"[éåçï\t\n\r]", "");
(as it is, it will also work without the @-sign)
Avatar of shieldguy

ASKER

what is the best way of removing all special characters like this

^
Ã
¨
¬
"
â

©
I mean their are some character that are not currently on the list but might appear in the future I want to right the code which will remove them all

Thanks
So, do you want to remove any character that is not a letter or number, or what? That would be pretty easy to implement.
I only need to allow the following character in my string and i need to remove any characters other the given below
a-z
A-Z
0-9
.
"
@
#
:
?
,
<
>
=
£
$
*
(
)
+
-
?

Thanks
I'll give you a half-psuedocode half-code method to call. You'll have to fill in a couple blanks (easily done).

public static System.String FilterString( System.String myString )
{
	System.Text.StringBuilder bldr = new StringBuilder();
	Byte currentChar = 0;
 
	foreach(System.Char chr in myString)
	{
	         try
		{
		// This ensures that the value is ASCII (0-255)
		       checked {currentChar = (Byte)chr;}
		     if((currentChar >= 65 && currentChar <= 90) || 
		        (currentChar >= 97 && currentChar <= 122) ||
		        (currentChar >= 48 && currentChar <= 57)
		/*continue same pattern, checking ASCII value.*/)
			{
				bldr.Append(chr);
			}
		}
		catch(System.OverflowException)
		{ continue; }
	}
 
	return bldr.ToString();
}

Open in new window

Of course, you can also use RegEx, which would be much easier, but that would kill the learning experience :) let me know if you'd rather use that, instead.
>  what is the best way of removing all special characters like this
I believe I just showed you, shortly before you posted that comment. Place the characters in that string and you should be fine. Other methods can of course also be used.
I just realize that best approach for me is to intead of removing all the selected character i would only have a code which will  only allow the characters given below
a-z
A-Z
0-9
.
"
@
#
:
?
,
<
>
=
£
$
*
(
)
+
-
?

If you can provide me the RegEx code for this it will be great
Thanks
abel:
That would work, but seeing that there are over 65,000 "valid" characters, I don't think typing out all of those is quite approprioate, or realistic.
> That would work, but seeing that there are over 65,000 "valid" characters

you can create ranges, of course. But the new approach as proposed by the OP is only allowing certain characters, which is what we call a "negated regex class", i.e. anything not between the brackets, excluding the negation-character ^ is removed:

string cleanString = Regex.Replace(dirtyString, @"[^a-zA-Z0-9."@#:?,<>=£$*()+-?], "");
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tx for the grade & pts but...
it would have been fair to split points, after all, the presented final solution has been a joined effort between Fernando and me.

If you think so too, you can click the "Request Attention" link and explain what split you want and some moderator will help out.
I really appreciate all the efforst put in by experts in this post

Thanks
@shieldguy: tx for trying and getting this right, but now that we are getting this right, let's do it the way it should be done: splitting points can be done by clicking on the "Accept multiple solutions" link that is next to each comments, not by clicking "Accept and award points" on your own comments.

To try to keep the archive correct, it is important that the right answer is selected as answer. I won't object here, but if you want to correct this, you can do this yourself by cancelling the closing process and selecting only the answers that really are just that: answers (not including your own comments).
??? what's happening now? While I was typing you stopped the closing process and accepted another comment? And no splitting anymore? That was not the idea, I liked it the other way around better: either split, or, well, split... :S
I dont understand what you mean I havent award anwer to myself I clearly select your and fernando posts and award 250 each

If their is error on site what can I do
You are making it hard for me
Let me explain to you
As I dont have the whole day need to do some work

You asked me to award multiple answer

I did send request
I get the response asking me to split the answers
I open the post select multiple answers and then award 250 each to you and fernando
after sumbit i can see the that the comment need to be added now to close the question i did
then I can still see the accept solution button without any multiple accept so I select the answer which I think I should select as best answer even after giving the point two both of you so I did

Now I dont want to stay more with this issue
If anyone not happy please award the points to anyone who you should think get it

Thanks

> As I dont have the whole day need to do some work

please don't get picky, we are all volunteers here and we too have work. The only thing we ask in return for our help is to award the points fairly. So please take the time to try to understand the EE awards process and read this on closing questions.

You are apparently caught up in the unhandiness of the interface. Your first attempt was close to perfect, but you didn't need to add your own comment. You are not the first that has some trouble with it. Since you are already around here for a long time, I didn't think that I should've left you additional instructions (normally I do), apologies for making this hard on you.

> Now I dont want to stay more with this issue

that's a pity, see above. But I don't want to estrange you from EE. Hopefully on some next questions you'll know how to split & award points.
I requested attention and explained the misunderstanding. Please split points next time the first time around, it will save us a lot of time which we can spend in helping you on real matters.
PS, when you said "I dont understand what you mean I havent award anwer to myself I clearly select your and fernando posts and award 250 each "

that was actually perfect, perhaps there was an error with the site after all.  ;-)
Mate thanks for understanding

I know you are trying to do the good thing over here as I had awarded all the points to you at first but you stuck to what is right and I appreciate that

Good work
HI shieldguy;

You have now awarded all points to me which is not what should have been. Can you please click on "Request Attention" at the top of this page and ask for the question to bre reopened to award points to abel and myself.

To Splits Points:
You split the points. Each comment box has a button that says Accept Multiple Solutions. Click that, and you will see a page that allows you to assign points to any of the comments in the thread. There is a grade box at the bottom of the page.

Thanks;
Fernando

Thank you and as always glad to have helped.  ;=)
Thanks Vee_Mod, you got it right! and thanks, Fernando (the r/a was done by me this time, see comments).
Thank you able.  ;=)