Skip to content

Compare strings and list different characters


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:



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


Leave a Comment

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: