ソースの表示以前のリビジョンバックリンク全て展開する/折り畳む文書の先頭へ Share via Share via... Twitter LinkedIn Facebook Pinterest Telegram WhatsApp Yammer Reddit Teams最近の変更Send via e-Mail印刷パーマリンク × « TOSHIBA MQ01ABB200R購入 1年以上放置してたWindows Updateが全然進まない件→解決 » C#のstring.Trim()は全角スペースまで削って下さりやがる C#のstringクラスにあるTrim()メソッドは、C#の三大便利関数の1つと言って良いくらい便利な関数だ。 言わずもがな、文字列の先頭と末尾に付随する空白を削除してくれる関数であるが、空白って一体なんなのよというと「Unicodeが定める空白文字」である。従って、Trim()は文字列の前後からUnicodeが定める空白文字を削除する関数という事になる。(更に言うと.NET Frameworkのバージョンによって細部の挙動が違う。詳細はMSDNを参照の事。) んじゃんじゃUnicodeの空白文字ってなんぞ?というと、C#的にはChar.IsWhiteSpace(letter) == trueとなる文字である。詳細はUnicodeの規格書なりWikipediaなりを見て頂くとして、true判定になる文字には半角スペース(U+0020)やタブ文字(U+0009)は然ることながら、全角スペース(U+3000)も含まれるのだ。つまりTrim()を使うと全角スペースも奇麗さっぱりなくなっちゃう。なんというか、C/C++の非WIDE文字な文字列操作に慣れている身からすると、直感とは異なる挙動なわけ。 今回はこれにハマった。削られちゃマズい全角スペースが見事になくなってて、Trim()の挙動を初めて知ったという(´・ω・`) 回避策は引数ありバージョンのTrim()で、全角スペースを除いた空白文字配列を渡すしかない模様。↓こんな感じで拡張メソッド化しておくと便利に使えるよ(`・ω・´) public static class MyStringAdditions { static char[] WhiteSpaceDelimiters = new char[] { '\u0009', // CHARACTER TABULATION '\u000A', // LINE FEED '\u000B', // LINE TABULATION '\u000C', // FORM FEED '\u000D', // CARRIAGE RETURN '\u0020', // SPACE '\u00A0', // NO-BREAK SPACE '\u2000', // EN QUAD '\u2001', // EM QUAD '\u2002', // EN SPACE '\u2003', // EM SPACE '\u2004', // THREE-PER-EM SPACE '\u2005', // FOUR-PER-EM SPACE '\u2006', // SIX-PER-EM SPACE '\u2007', // FIGURE SPACE '\u2008', // PUNCTUATION SPACE '\u2009', // THIN SPACE '\u200A', // HAIR SPACE '\u200B', // ZERO WIDTH SPACE // '\u3000', // IDEOGRAPHIC SPACE -- これが所謂全角スペース '\uFEFF' // ZERO WIDTH NO-BREAK SPACE }; public static string TrimWithoutZenkakuSpace(this string str) { string s = str.Trim(WhiteSpaceDelimiters); return s; } } この件とは直接関係ないけど、TrimStartとTrimEndなんてメソッドもあったんだね。取り除きたい文字の配列を渡すと、対象文字列の先頭もしくは末尾から除去してくれる。Trimの分割バージョンみたいなやつ、というよりもTrimがTrimStartとTrimEndの合体技と言った方がいいか。覚えといて損は無さそう。 参考サイト String.Trim メソッド (System) .NET Reference Guide | Trimming Character Strings | InformIT Comments Phantom Wallet has quickly 文字をbecome a favorite for users engaging with the Solana blockchain. Known for its speed, simplicity, and robust security, the Phantom Wallet Extension https://sites.google.com/phantomwalletus.com/phantom-wallet/home https://sites.google.com/wallcrypus.com/uphold-login/home 1 | oliaaaaa | 2025-08-07 15:29 | reply pop over to this website https://lagoon.icu 2 | Melvinlem | 2025-08-17 10:42 | reply look at here now https://sailor.wtf/ 3 | WarrenTrism | 2025-08-18 23:08 | reply my latest blog post https://lavarage.cc/ 4 | Jamesalicy | 2025-08-18 23:08 | reply i loved this https://extended-exchange.cc/ 5 | DavidJem | 2025-08-19 06:50 | reply Phantom Wallet has quickly become a favorite for users engaging with the Solana blockchain. Known for its speed, simplicity, and robust security, the Phantom Wallet Extension. https://sites.google.com/view/phantomwalletextensiondownload/home 6 | james cliftonm | 2025-08-30 21:51 | reply Having trouble logging into Crypto.com? Discover solutions for common login issues like password errors, 2FA problems, and app glitches. Learn how to reset your credentials, update the app, or contact support. This quick guide helps you fix login problems and regain secure access to your Crypto.com account on both mobile and desktop platforms. https://sites.google.com/cryptissue.com/cryptologin-issues/home https://sites.google.com/cryptissue.com/cryptologin-issue/home 7 | willam | 2025-10-17 19:09 | reply PayPal Login allows users to securely access their digital wallet and manage online payments with ease. Through strong encryption and multi-factor authentication, PayPal ensures safe transactions, account monitoring, and quick money transfers. Logging in offers users complete control over balances, purchase history, and linked cards or bank accounts. https://sites.google.com/abcwaps.com/paypal-login/home https://sites.google.com/abcwaps.com/paypallogin/home 8 | tuco | 2025-12-02 13:22 | reply Name E-Mail Website 人間の証明として、ボックス内の全ての文字を入力してください。 この項目は空のままにして下さい:Preview Comment blog/2015/2015-09-17.1483514390.txt.gz 最終更新: 2017-01-04 16:19by Decomo