2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Powershell シングルクォーテーションやダブルクォーテーションのANSIコードを取得する

Last updated at Posted at 2022-11-14

参考文献

06.指定した文字のANSI文字コードを取得するには - HIRO'sNET

単純なようだが

[byte][string]"a"

image.png

これはエラーです。CharとStringはあまり違いが意識されないのですが、この場合Stringではエラーになります。
これは記事の中に書いてあります。

ANSIコードはバイト値として取得する必要がありますが、String型を直接バイト値に変換することはできません。
そこで Char型にキャストをしてから、バイト値に変換(キャスト)する必要があります。

[byte][char]'a'
[byte][string]"a"

どちらも97です。

それではシングルクォーテーションやダブルクォーテーションはどう書くか

前回のトリビアでも参考にしましたが、
about_Quoting_Rules
How to express the dollar sign in string

答え

[byte][char]'"'  # Double Quotation
[byte][char]'''' # Single Quotation
[byte][char]'`'  # Backquote

曲者はシングルクォーテーション

これは4回必要になります。
このため

'Don't'

'Don''t'

このように書きます。ここからシングルクォーテーションを4つというのがわかります。

Powershellがマイナーな理由がなんとなく

 しかし、この結論はかなり変です。英文では、Don'tのような表現は普通にあります。にも関わらず、Powershellでは毎回エスケープする必要があります。普通の英文を気軽にコピペして文字列に代入することもできません。HIRO's NET も HIRO``sNETと書くことになります。
 このようにユーザーに負担をかけるような言語は普通は使い物になりません。なにか回避方法があるのでしょうか。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?