Skip to content

Compare strings and list different characters

2012/01/03

I had two strings that should be the same. But they were not. A

 $string1 -eq $string2

was false. There were unprinted characters that made them different, because just printing them showed no difference. So I converted them to CharArrays and compared them char by char, listing the difference:

(125 is the length of the longest string)

$l = $string1.ToCharArray()

$o = $string2.ToCharArray()

for($i=0;$i -lt 125; $i++) {if($l[$i] -ne $o[$i]) {“->{0};{1:D2};{2:D2};” -f $i, [int]$l[$i], [int]$o[$i]} }

That came out with:

->83;13;10;
->84;10;60;
->85;60;112;
->86;112;62;
->87;62;50;
->88;50;110;
->89;110;100;
->90;100;32;

…..

The difference becomes visible: one uses Carrige Return + Linefeed and the other string only used LineFeed to create a second line.

 

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: