[topic] 末尾の空白文字を取り除く

与えられた文字列の末尾の空白文字を取り除く方法と、その操作が与えられた文字列を破壊するかどうか。取り除かれる空白文字の種類。

Posted feedbacks - C#

C# では String.TrimEnd メソッドがあります。非破壊的。 取り除かれる文字を引数に指定すると、その文字を取り除きます。 null を指定すると、Unicode の空白文字 = {0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x20, 0xA0, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x200B, 0x3000, 0xFEFF} の21個が消えます。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using System;
static class Program {
    static void Main() {
        string text = "abcdefg\n   \n   \n\t\t\t";
        Console.WriteLine("[{0}]", text.TrimEnd(null));
            // [abcdefg]
        Console.WriteLine("[{0}]", text.TrimEnd('\n', '\r', ' ', ' ', '\t', 'g', 'f'));
            // [abcde]
    }
}

Index

Feed

Other

Link

Pathtraq

loading...