Question

Imagine I have String in C#: "I Don’t see ya.."

I want to remove (replace to nothing or etc.) these "’" symbols.

How do I do this?

Was it helpful?

Solution

That 'junk' looks a lot like someone interpreted UTF-8 data as ISO 8859-1 or Windows-1252, probably repeatedly.

’ is the sequence C3 A2, E2 82 AC, E2 84 A2.

  • UTF-8 C3 A2 = U+00E2 = â
  • UTF-8 E2 82 AC = U+20AC = €
  • UTF-8 E2 84 A2 = U+2122 = ™

We then do it again: in Windows 1252 this sequence is E2 80 99, so the character should have been U+2019, RIGHT SINGLE QUOTATION MARK (’)

You could make multiple passes with byte arrays, Encoding.UTF8 and Encoding.GetEncoding(1252) to correctly turn the junk back into what was originally entered. You will need to check your processing to find the two places that UTF-8 data was incorrectly interpreted as Windows-1252.

OTHER TIPS

"I Don’t see ya..".Replace( "’", string.Empty);

How did that junk get in there the first place? That's the real question.

By removing any non-latin character you'll be intentionally breaking some internationalization support.

Don't forget the poor guy who's name has a "â" in it.

This looks disturbingly familiar to a character encoding issue dealing with the Windows character set being stored in a database using the standard character encoding. I see someone voted Will down, but he has a point. You may be solving the immediate issue, but the combinations of characters are limitless if this is the issue.

If you really have to do this, regular expressions are probably the best solution.

I would strongly recommend that you think about why you have to do this, though - at least some of the characters your listing as undesirable are perfectly valid and useful in other languages, and just filtering them out will most likely annoy at least some of your international users. As a swede, I can't emphasize enough how much I hate systems that can't handle our å, ä and ö characters correctly.

Consider Regex.Replace(your_string, regex, "") - that's what I use.

Test each character in turn to see if it is a valid alphabetic or numeric character and if not then remove it from the string. The character test is very simple, just use...

char.IsLetterOrDigit;

Please there are various others such as...

char.IsSymbol;
char.IsControl;

Regex.Replace("The string", "[^a-zA-Z ]","");

That's how you'd do it in C#, although that regular expression ([^a-zA-Z ]) should work in most languages.

[Edited: forgot the space in the regex]

The ASCII / Integer code for these characters would be out of the normal alphabetic Ranges. Seek and replace with empty characters. String has a Replace method I believe.

Either use a blacklist of stuff you do not want, or preferably a white list (set). With a white list you iterate over the string and only copy the letters that are in your white list to the result string. You said remove, and the way you do that is having two pointers one you read from (R) and one you write to (W):

I Donââ‚
     W  R

if comma is in your whitelist then you would in this case read the comma and write it where à is then advance both pointers. UTF-8 is a multi-byte encoding, so you advancing the pointer may not just be adding to the address.

With C an easy to way to get a white list by using one of the predefined functions (or macros): isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit. In this case you send up with a white list function instead of a set of course.

Usually when I see data like you have I look for memory corruption, or evidence to suggest that the encoding I expect is different than the one the data was entered with.

/Allan

I had the same problem with extraneous junk thrown in by adobe in an EXIF dump. I spent an hour looking for a straight answer and trying numerous half-baked suggestions which did not work here.

This thread more than most I have read was replete with deep, probing questions like 'how did it get there?', 'what if somebody has this character in their name?', 'are you sure you want to break internationalization?'.

There were some impressive displays of erudition positing how this junk could have gotten here and explaining the evolution of the various character encoding schemes. The person wanted to know how to remove it, not how it came to be or what the standards orgs are up to, interesting as this trivia may be.

I wrote a tiny program which gave me the right answer. Instead of paraphrasing the main concept, here is the entire, self-contained, working (at least on my system) program and the output I used to nuke the junk:

#!/usr/local/bin/perl -w

# This runs in a dos window and shows the char, integer and hex values
# for the weird chars. Install the HEX values in the REGEXP below until
# the final test line looks normal. 
$str = 's: “Brian';  # Nuke the 3 werid chars in front of Brian.
@str = split(//, $str);
printf("len str '$str' = %d, scalar \@str = %d\n", 
    length $str, scalar @str);
$ii = -1;
foreach $c (@str)  {
   $ii++;
   printf("$ii) char '$c', ord=%03d, hex='%s'\n", 
       ord($c), unpack("H*", $c));
}
# Take the hex characters shown above, plug them into the below regexp
# until the junk disappears!
($s2 = $str) =~ s/[\xE2\x80\x9C]//g;  # << Insert HEX values HERE
print("S2=>$s2<\n");  # Final test

Result:
M:\new\6s-2014.1031-nef.halloween>nuke_junk.pl
len str 's: GÇ£Brian' = 11, scalar @str = 11
0) char 's', ord=115, hex='73'
1) char ':', ord=058, hex='3a'
2) char ' ', ord=032, hex='20'
3) char 'G', ord=226, hex='e2'
4) char 'Ç', ord=128, hex='80'
5) char '£', ord=156, hex='9c'
6) char 'B', ord=066, hex='42'
7) char 'r', ord=114, hex='72'
8) char 'i', ord=105, hex='69'
9) char 'a', ord=097, hex='61'
10) char 'n', ord=110, hex='6e'
S2=>s: Brian<

It's NORMAL!!!

One other actionable, working suggestion I ran across: iconv -c -t ASCII < 6s-2014.1031-238246.halloween.exf.dif > exf.ascii.dif

If String having the any Junk date , This is good to way remove those junk date

     string InputString = "This is grate kingdom¢Ã‚¬â"; 
     string replace = "’";
     string OutputString= Regex.Replace(InputString, replace, "");

     //OutputString having the following result 

It's working good to me , thanks for looking this review.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top