Share via


String.TrimEnd メソッド

このインスタンスの末尾から、配列で指定された文字セットをすべて削除します。

Public Function TrimEnd( _
   ByVal ParamArray trimChars() As Char _) As String
[C#]
public string TrimEnd(   params char[] trimChars);
[C++]
public: String* TrimEnd(__wchar_ttrimChars __gc[]);
[JScript]
public function TrimEnd(
   trimChars : Char[]) : String;

パラメータ

  • trimChars
    削除する Unicode 文字の配列、または null 参照 (Visual Basic では Nothing) 。

戻り値

出現する trimChars の文字を末尾からすべて削除した後の StringtrimChars が null 参照 (Visual Basic では Nothing) の場合は、代わりに空白文字が削除されます。

解説

どの Unicode 文字が空白文字として分類されているかの詳細については、 Char.IsWhiteSpace メソッドの解説を参照してください。

使用例

[Visual Basic, C#, C++] TrimEnd メソッドを使用して、文字列の末尾から空白やその他の文字を削除する方法については、次のコード例を参照してください。

 
Imports System

Public Class TrimTest
    
    Public Shared Sub Main()
        Dim temp As String() = MakeArray()
        
        
        Console.WriteLine("Concatenating the inital values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
        Dim i As Integer
        
        ' trim whitespace from both ends of the elements
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).Trim()
        
        Next i
        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
        
        ' reset the array
        temp = MakeArray()
        
        ' trim the start of the elements. Passing null trims whitespace only
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).TrimStart(" "c)
        Next i

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)

        ' reset the array
        temp = MakeArray()

        ' trim the end of the elements. Passing null trims whitespace only
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).TrimEnd(" "c)
        Next i

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'", [String].Concat(temp))
    End Sub 'Main

    Private Shared Function MakeArray() As String()
        Dim arr As String() = {"  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "}
        Return arr
    End Function 'MakeArray
End Class 'TrimTest

[C#] 
using System;

public class TrimTest {
    public static void Main() {

        string [] temp = MakeArray();

        Console.WriteLine("Concatenating the inital values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // trim whitespace from both ends of the elements
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].Trim();

        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the start of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimStart(null);

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the end of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimEnd(null);

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'", String.Concat(temp));
    }

    private static string [] MakeArray() {
        string [] arr = {"  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "};
        return arr;
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

String* MakeArray()[]
{
   String* arr[] = {S"  please    ", S"  tell    ", S"  me    ", S"  about    ", S"  yourself    "};
   return arr;
}

int main()
{

   String* temp[] = MakeArray();

   Console::WriteLine(S"Concatenating the inital values in the array, we get the string:");
   Console::WriteLine(S"'{0}'\n", String::Concat(temp));

   // trim whitespace from both ends of the elements
   for (int i = 0; i < temp->Length; i++)
      temp->Item[i] = temp[i]->Trim();

   Console::WriteLine(S"Concatenating the trimmed values in the array, we get the string:");
   Console::WriteLine(S"'{0}'", String::Concat(temp));

   // reset the array
   temp = MakeArray();

   // trim the start of the elements-> Passing 0 trims whitespace only
   for (int i = 0; i < temp->Length; i++)
      temp->Item[i] = temp[i]->TrimStart(0);

   Console::WriteLine(S"Concatenating the start-trimmed values in the array, we get the string:");
   Console::WriteLine(S"'{0}'\n", String::Concat(temp));

   // reset the array
   temp = MakeArray();

   // trim the end of the elements-> Passing 0 trims whitespace only
   for (int i = 0; i < temp->Length; i++)
      temp->Item[i] = temp[i]->TrimEnd(0);

   Console::WriteLine(S"Concatenating the end-trimmed values in the array, we get the string:");
   Console::WriteLine(S"'{0}'", String::Concat(temp));
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | Char | Trim | TrimStart