String.Trim メソッド
定義
現在の文字列から、指定した文字セットを先頭および末尾の出現箇所からすべて削除した新しい文字列を返します。Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current string are removed.
オーバーロード
Trim(Char[]) |
現在の文字列から、配列で指定された一連の文字が先頭および末尾に現れる箇所をすべて削除します。Removes all leading and trailing occurrences of a set of characters specified in an array from the current string. |
Trim(Char) |
現在の文字列から、文字の先頭および末尾のインスタンスをすべて削除します。Removes all leading and trailing instances of a character from the current string. |
Trim() |
現在の文字列から、先頭および末尾にある空白文字をすべて削除します。Removes all leading and trailing white-space characters from the current string. |
Trim(Char[])
現在の文字列から、配列で指定された一連の文字が先頭および末尾に現れる箇所をすべて削除します。Removes all leading and trailing occurrences of a set of characters specified in an array from the current string.
public:
System::String ^ Trim(... cli::array <char> ^ trimChars);
public string Trim (params char[] trimChars);
public string Trim (params char[]? trimChars);
member this.Trim : char[] -> string
Public Function Trim (ParamArray trimChars As Char()) As String
パラメーター
- trimChars
- Char[]
削除する Unicode 文字の配列、または null
。An array of Unicode characters to remove, or null
.
戻り値
現在の文字列の先頭および末尾から、trimChars
パラメーターの文字をすべて削除した後に残った文字列。The string that remains after all occurrences of the characters in the trimChars
parameter are removed from the start and end of the current string. trimChars
が null
または空の配列の場合は、代わりに空白文字が削除されます。If trimChars
is null
or an empty array, white-space characters are removed instead. 現在のインスタンスから文字をトリミングできない場合、メソッドは現在のインスタンスを変更せずに返します。If no characters can be trimmed from the current instance, the method returns the current instance unchanged.
例
次の例では、メソッドを使用して、 Trim(System.Char[])
文字列からスペース、アスタリスク (*)、およびアポストロフィ (') 文字を削除します。The following example uses the Trim(System.Char[])
method to remove space, asterisk (*), and apostrophe (') characters from a string.
using namespace System;
void main()
{
array<Char>^ charsToTrim = { L'*', L' ', L'\\' };
String^ banner = "*** Much Ado About Nothing ***";
String^ result = banner->Trim(charsToTrim);
Console::WriteLine("Trimmmed\n {0}\nto\n '{1}'", banner, result);
}
// The example displays the following output:
// Trimmmed
// *** Much Ado About Nothing ***
// to
// 'Much Ado About Nothing'
char[] charsToTrim = { '*', ' ', '\''};
string banner = "*** Much Ado About Nothing ***";
string result = banner.Trim(charsToTrim);
Console.WriteLine("Trimmmed\n {0}\nto\n '{1}'", banner, result);
// The example displays the following output:
// Trimmmed
// *** Much Ado About Nothing ***
// to
// 'Much Ado About Nothing'
Module Example
Public Sub Main()
Dim charsToTrim() As Char = { "*"c, " "c, "'"c}
Dim banner As String = "*** Much Ado About Nothing ***"
Dim result As String = banner.Trim(charsToTrim)
Console.WriteLine("Trimmmed{0} {1}{0}to{0} '{2}'", _
vbCrLf, banner, result)
End Sub
End Module
' The example displays the following output:
' Trimmmed
' *** Much Ado About Nothing ***
' to
' 'Much Ado About Nothing'
注釈
メソッドは、 Trim(System.Char[])
パラメーターに含まれる先頭と末尾のすべての文字を現在の文字列から削除し trimChars
ます。The Trim(System.Char[])
method removes from the current string all leading and trailing characters that are in the trimChars
parameter. に含まれていない文字が検出されると、それぞれの先頭および末尾のトリム操作 trimChars
が停止します。Each leading and trailing trim operation stops when a character that is not in trimChars
is encountered. たとえば、現在の文字列が "123abc456xyz789" で、 trimChars
"1" ~ "9" の数字が含まれている場合、 Trim(System.Char[])
メソッドは "abc456xyz" を返します。For example, if the current string is "123abc456xyz789" and trimChars
contains the digits from "1" through "9", the Trim(System.Char[])
method returns "abc456xyz".
注意
メソッドが Trim(System.Char[])
現在のインスタンスから任意の文字を削除した場合、このメソッドは現在のインスタンスの値を変更しません。If the Trim(System.Char[])
method removes any characters from the current instance, this method does not modify the value of the current instance. 代わりに、現在のインスタンスに含まれる先頭および末尾のすべての文字が削除された新しい文字列を返し trimChars
ます。Instead, it returns a new string in which all leading and trailing trimChars
characters found in the current instance are removed.
現在の文字列がと等しい場合、 Empty または現在のインスタンス内のすべての文字が配列内の文字で構成されている場合 trimChars
、メソッドはを返し Empty ます。If the current string equals Empty or all the characters in the current instance consist of characters in the trimChars
array, the method returns Empty.
trimChars
がまたは空の配列の場合 null
、このメソッドは、メソッド true
に渡されたときにメソッドによって返される先頭または末尾の文字を削除し Char.IsWhiteSpace ます。If trimChars
is null
or an empty array, this method removes any leading or trailing characters that result in the method returning true
when they are passed to the Char.IsWhiteSpace method.
注意 (呼び出し元)
以前のバージョンでは、 .NET Framework 3.5 SP1.NET Framework 3.5 SP1 trimChars
がまたは空の配列の場合に、このメソッドがトリムする空白文字の内部リストを保持し null
ます。The .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions maintains an internal list of white-space characters that this method trims if trimChars
is null
or an empty array. 以降で .NET Framework 4.NET Framework 4 は、がまたは空の配列の場合、 trimChars
null
メソッドはすべての Unicode 空白文字 (つまり、 true
メソッドに渡されるときに戻り値を生成する文字) をトリムし IsWhiteSpace(Char) ます。Starting with the .NET Framework 4.NET Framework 4, if trimChars
is null
or an empty array, the method trims all Unicode white-space characters (that is, characters that produce a true
return value when they are passed to the IsWhiteSpace(Char) method). この変更により、 Trim() 以前のバージョンのメソッドでは、 .NET Framework 3.5 SP1.NET Framework 3.5 SP1 2 つの文字、0の幅空間 (u + 200B) とゼロ幅の改行なしスペース (u + FEFF) が削除 Trim() され、以降のバージョンのメソッドは .NET Framework 4.NET Framework 4 削除されません。Because of this change, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions removes two characters, ZERO WIDTH SPACE (U+200B) and ZERO WIDTH NO-BREAK SPACE (U+FEFF), that the Trim() method in the .NET Framework 4.NET Framework 4and later versions does not remove. さらに、 Trim() 以前のバージョンのメソッドで .NET Framework 3.5 SP1.NET Framework 3.5 SP1 は、3つの Unicode の空白文字 (モンゴル語の母音区切り記号 (u + 180 e)、ナロー改行なしスペース (u + 202f)、中かっこ (u + 205F)) はトリムされません。In addition, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions does not trim three Unicode white-space characters: MONGOLIAN VOWEL SEPARATOR (U+180E), NARROW NO-BREAK SPACE (U+202F), and MEDIUM MATHEMATICAL SPACE (U+205F).
こちらもご覧ください
適用対象
Trim(Char)
現在の文字列から、文字の先頭および末尾のインスタンスをすべて削除します。Removes all leading and trailing instances of a character from the current string.
public:
System::String ^ Trim(char trimChar);
public string Trim (char trimChar);
member this.Trim : char -> string
Public Function Trim (trimChar As Char) As String
パラメーター
- trimChar
- Char
削除する Unicode 文字。A Unicode character to remove.
戻り値
現在の文字列の先頭と末尾から trimChar
文字のすべてのインスタンスが削除された後に残った文字列。The string that remains after all instances of the trimChar
character are removed from the start and end of the current string. 現在のインスタンスから文字をトリミングできない場合、メソッドは現在のインスタンスを変更せずに返します。If no characters can be trimmed from the current instance, the method returns the current instance unchanged.
注釈
メソッドは、 Trim(System.Char)
文字の先頭および末尾のすべてのインスタンスを現在の文字列から削除し trimChar
ます。The Trim(System.Char)
method removes from the current string all leading and trailing instances of the trimChar
character. それぞれの先頭および末尾のトリム操作は、とは異なる文字 trimChar
が検出されると停止します。Each leading and trailing trim operation stops when a character different from trimChar
is encountered. たとえば、がで trimChar
、 -
現在の文字列が "---abc---xyz----" の場合、この Trim(System.Char)
メソッドは "abc---xyz" を返します。For example, if trimChar
is -
and the current string is "---abc---xyz----", the Trim(System.Char)
method returns "abc---xyz". 文字列内の単語間の文字を削除するには、 .net の正規表現を使用します。To remove characters between words in a string, use .NET Regular Expressions.
注意
メソッドが Trim(System.Char)
現在のインスタンスから任意の文字を削除した場合、このメソッドは現在のインスタンスの値を変更しません。If the Trim(System.Char)
method removes any characters from the current instance, this method does not modify the value of the current instance. 代わりに、現在のインスタンスに含まれる先頭および末尾のすべての文字が削除された新しい文字列を返し trimChar
ます。Instead, it returns a new string in which all leading and trailing trimChar
characters found in the current instance are removed.
現在の文字列がと等しい場合、または現在のインスタンス内のすべての文字が文字で構成されている場合 Empty trimChar
、メソッドはを返し Empty ます。If the current string equals Empty or all the characters in the current instance consist of trimChar
characters, the method returns Empty.
適用対象
Trim()
現在の文字列から、先頭および末尾にある空白文字をすべて削除します。Removes all leading and trailing white-space characters from the current string.
public:
System::String ^ Trim();
public string Trim ();
member this.Trim : unit -> string
Public Function Trim () As String
戻り値
現在の文字列の先頭および末尾から空白文字をすべて削除した後に残った文字列。The string that remains after all white-space characters are removed from the start and end of the current string. 現在のインスタンスから文字をトリミングできない場合、メソッドは現在のインスタンスを変更せずに返します。If no characters can be trimmed from the current instance, the method returns the current instance unchanged.
例
次の例では、メソッドを使用し String.Trim() て、連結前にユーザーが入力した文字列から余分な空白を削除します。The following example uses the String.Trim() method to remove any extra white space from strings entered by the user before concatenating them.
using namespace System;
void main()
{
Console::Write("Enter your first name: ");
String^ firstName = Console::ReadLine();
Console::Write("Enter your middle name or initial: ");
String^ middleName = Console::ReadLine();
Console::Write("Enter your last name: ");
String^ lastName = Console::ReadLine();
Console::WriteLine();
Console::WriteLine("You entered '{0}', '{1}', and '{2}'.",
firstName, middleName, lastName);
String^ name = ((firstName->Trim() + " " + middleName->Trim())->Trim() + " " +
lastName->Trim())->Trim();
Console::WriteLine("The result is " + name + ".");
}
// The following is possible output from this example:
// Enter your first name: John
// Enter your middle name or initial:
// Enter your last name: Doe
//
// You entered ' John ', '', and ' Doe'.
// The result is John Doe.
using System;
public class Example
{
public static void Main()
{
Console.Write("Enter your first name: ");
string firstName = Console.ReadLine();
Console.Write("Enter your middle name or initial: ");
string middleName = Console.ReadLine();
Console.Write("Enter your last name: ");
string lastName = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("You entered '{0}', '{1}', and '{2}'.",
firstName, middleName, lastName);
string name = ((firstName.Trim() + " " + middleName.Trim()).Trim() + " " +
lastName.Trim()).Trim();
Console.WriteLine("The result is " + name + ".");
// The following is a possible output from this example:
// Enter your first name: John
// Enter your middle name or initial:
// Enter your last name: Doe
//
// You entered ' John ', '', and ' Doe'.
// The result is John Doe.
}
}
Module Example
Public Sub Main()
Console.Write("Enter your first name: ")
Dim firstName As String = Console.ReadLine()
Console.Write("Enter your middle name or initial: ")
Dim middleName As String = Console.ReadLine()
Console.Write("Enter your last name: ")
Dim lastName As String = Console.ReadLine
Console.WriteLine()
Console.WriteLine("You entered '{0}', '{1}', and '{2}'.", _
firstName, middleName, lastName)
Dim name As String = ((firstName.Trim() + " " + middleName.Trim()).Trim() _
+ " " + lastName.Trim()).Trim()
Console.WriteLine("The result is " + name + ".")
End Sub
End Module
' The following is possible output from this example:
' Enter your first name: John
' Enter your middle name or initial:
' Enter your last name: Doe
'
' You entered ' John ', '', and ' Doe'.
' The result is John Doe.
注釈
メソッドは、 Trim
現在の文字列からすべての先頭および末尾の空白文字を削除します。The Trim
method removes from the current string all leading and trailing white-space characters. それぞれの先頭および末尾のトリム操作は、空白以外の文字が検出されると停止します。Each leading and trailing trim operation stops when a non-white-space character is encountered. たとえば、現在の文字列が "abc" の場合、この Trim
メソッドは "abc xyz" を返します。For example, if the current string is " abc xyz ", the Trim
method returns "abc xyz". 文字列内の単語間の空白文字を削除するには、 .net の正規表現を使用します。To remove white-space characters between words in a string, use .NET Regular Expressions.
注意
メソッドが Trim
現在のインスタンスから任意の文字を削除した場合、このメソッドは現在のインスタンスの値を変更しません。If the Trim
method removes any characters from the current instance, this method does not modify the value of the current instance. 代わりに、現在のインスタンスで見つかった先頭および末尾の空白文字がすべて削除された新しい文字列を返します。Instead, it returns a new string in which all leading and trailing white space characters found in the current instance are removed.
現在の文字列がと等しい場合、 Empty または現在のインスタンス内のすべての文字が空白文字で構成されている場合、メソッドはを返し Empty ます。If the current string equals Empty or all the characters in the current instance consist of white-space characters, the method returns Empty.
空白文字は、Unicode 規格で定義されています。White-space characters are defined by the Unicode standard. メソッドは、 Trim
メソッドに渡されるときにの戻り値を生成する先頭および末尾の文字を削除し true
Char.IsWhiteSpace ます。The Trim
method removes any leading and trailing characters that produce a return value of true
when they are passed to the Char.IsWhiteSpace method.
注意 (呼び出し元)
以前のバージョンでは、 .NET Framework 3.5 SP1.NET Framework 3.5 SP1 このメソッドがトリムする空白文字の内部リストを維持しています。The .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions maintain an internal list of white-space characters that this method trims. 以降では、 .NET Framework 4.NET Framework 4 メソッドは、すべての Unicode の空白文字 (つまり、 true
メソッドに渡されるときに戻り値を生成する文字) をトリムし IsWhiteSpace(Char) ます。Starting with the .NET Framework 4.NET Framework 4, the method trims all Unicode white-space characters (that is, characters that produce a true
return value when they are passed to the IsWhiteSpace(Char) method). この変更により、 Trim() 以前のバージョンのメソッドでは、 .NET Framework 3.5 SP1.NET Framework 3.5 SP1 2 つの文字、0の幅空間 (u + 200B) とゼロ幅の改行なしスペース (u + FEFF) が削除 Trim() され、以降のバージョンのメソッドは .NET Framework 4.NET Framework 4 削除されません。Because of this change, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions removes two characters, ZERO WIDTH SPACE (U+200B) and ZERO WIDTH NO-BREAK SPACE (U+FEFF), that the Trim() method in the .NET Framework 4.NET Framework 4and later versions does not remove. さらに、 Trim() 以前のバージョンのメソッドで .NET Framework 3.5 SP1.NET Framework 3.5 SP1 は、3つの Unicode の空白文字 (モンゴル語の母音区切り記号 (u + 180 e)、ナロー改行なしスペース (u + 202f)、中かっこ (u + 205F)) はトリムされません。In addition, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions does not trim three Unicode white-space characters: MONGOLIAN VOWEL SEPARATOR (U+180E), NARROW NO-BREAK SPACE (U+202F), and MEDIUM MATHEMATICAL SPACE (U+205F).