Home > Back-end >  How can you use a character comparison against a dash/hyphen character in Powershell?
How can you use a character comparison against a dash/hyphen character in Powershell?

Time:08-27

I am doing a character comparison and can't figure out why this does not work. I've tried different options listed below and have tried the hyphen near the backspace and the hyphen on the number pad. I tried to convert it to its char byte value and that fails. I've cast the '-' into a char variable and it still fails?

What's the solution for this?

[DBG]: PS C:\> $line.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                             
-------- -------- ----                                     --------                                                                                                                             
True     True     String                                   System.Object      
                                                                                                              
[DBG]: PS C:\> $line[$i]
–
[DBG]: PS C:\> $line[$i] -eq "-"
False
[DBG]: PS C:\> $line[$i] -eq "-"
False
[DBG]: PS C:\> [byte[]][char[]]"-"
45
[DBG]: PS C:\> [byte[]][char[]]$line[$i]
Cannot convert value "–" to type "System.Byte". Error: "Value was either too large or too small for an unsigned byte."
At line:1 char:1
  [byte[]][char[]]$line[$i]
  ~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidArgument: (:) [], RuntimeException
      FullyQualifiedErrorId : InvalidCastIConvertible

[DBG]: PS C:\> $line[$i] -eq "\-"
False
[DBG]: PS C:\> $line[$i] -eq "`-"
False
[DBG]: PS C:\> $line[$i] -eq '\-'
False
[DBG]: PS C:\> $line[$i] -eq '`-'
False
[DBG]: PS C:\> $line[$i] -like '-'
False
[DBG]: PS C:\> $line[$i] -like "-"
False
[DBG]: PS C:\> $line[$i] -match '-'
False
[DBG]: PS C:\> $line[$i] -match "-"
False
[DBG]: PS C:\> $line[$i].GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                             
-------- -------- ----                                     --------                                                                                                                             
True     True     Char                                     System.ValueType                                                                                                                     

[DBG]: PS C:\> $line[$i] | gm


   TypeName: System.Char

Name        MemberType Definition                                                                                                                                              
----        ---------- ----------                                                                                                                                              
CompareTo   Method     int CompareTo(System.Object value), int CompareTo(char value), int IComparable.CompareTo(System.Object obj), int IComparable[char].CompareTo(char other)
Equals      Method     bool Equals(System.Object obj), bool Equals(char obj), bool IEquatable[char].Equals(char other)                                                         
GetHashCode Method     int GetHashCode()                                                                                                                                       
GetType     Method     type GetType()                                                                                                                                          
GetTypeCode Method     System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()                                                                               
ToBoolean   Method     bool IConvertible.ToBoolean(System.IFormatProvider provider)                                                                                            
ToByte      Method     byte IConvertible.ToByte(System.IFormatProvider provider)                                                                                               
ToChar      Method     char IConvertible.ToChar(System.IFormatProvider provider)                                                                                               
ToDateTime  Method     datetime IConvertible.ToDateTime(System.IFormatProvider provider)                                                                                       
ToDecimal   Method     decimal IConvertible.ToDecimal(System.IFormatProvider provider)                                                                                         
ToDouble    Method     double IConvertible.ToDouble(System.IFormatProvider provider)                                                                                           
ToInt16     Method     int16 IConvertible.ToInt16(System.IFormatProvider provider)                                                                                             
ToInt32     Method     int IConvertible.ToInt32(System.IFormatProvider provider)                                                                                               
ToInt64     Method     long IConvertible.ToInt64(System.IFormatProvider provider)                                                                                              
ToSByte     Method     sbyte IConvertible.ToSByte(System.IFormatProvider provider)                                                                                             
ToSingle    Method     float IConvertible.ToSingle(System.IFormatProvider provider)                                                                                            
ToString    Method     string ToString(), string ToString(System.IFormatProvider provider), string IConvertible.ToString(System.IFormatProvider provider)                      
ToType      Method     System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)                                                                 
ToUInt16    Method     uint16 IConvertible.ToUInt16(System.IFormatProvider provider)                                                                                           
ToUInt32    Method     uint32 IConvertible.ToUInt32(System.IFormatProvider provider)                                                                                           
ToUInt64    Method     uint64 IConvertible.ToUInt64(System.IFormatProvider provider)                                                                                                                                                                                                                

[DBG]: PS C:\> [char]$ch = '-'
[DBG]: PS C:\> $ch.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                             
-------- -------- ----                                     --------                                                                                                                             
True     True     Char                                     System.ValueType                                                                                                                     


[DBG]: PS C:\> $line[$i] -eq $ch
False

CodePudding user response:

In case this comes up in a search and is helpful to someone, try a comparison as seen below

[DBG]: PS C:\> [char] 0x2014
—
[DBG]: PS C:\> [char] 0x2013
–
[DBG]: PS C:\> $line[$i] -eq ([char] 0x2014) # em-dash
False
[DBG]: PS C:\> $line[$i] -eq ([char] 0x2013) # en-dash
True

CodePudding user response:

A new script to id unicode chars:

# idChar.ps1

param($inputChar)

if (! (test-path $psscriptroot\UnicodeData.txt)) { 
  wget http://www.unicode.org/Public/UNIDATA/UnicodeData.txt -outfile UnicodeData.txt }
$unicode = import-csv $psscriptroot\UnicodeData.txt -Delimiter ';' -Header code,name
$unicode | ? { $inputChar[0] -eq [int]('0x'   $_.code) }
.\idChar –

code name
---- ----
2013 EN DASH


.\idChar —

code name
---- ----
2014 EM DASH


.\idChar -

code name
---- ----
002D HYPHEN-MINUS
  • Related