英語で読む

次の方法で共有


String.Remove メソッド

定義

現在の文字列から指定された文字数を削除した新しい文字列を返します。

オーバーロード

Remove(Int32)

現在のインスタンスの指定した位置から指定した最後の位置までの全文字が削除された新しい文字列を返します。

Remove(Int32, Int32)

現在のインスタンス内の指定した位置から指定した文字数が削除された新しい文字列を返します。

Remove(Int32)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

現在のインスタンスの指定した位置から指定した最後の位置までの全文字が削除された新しい文字列を返します。

C#
public string Remove (int startIndex);

パラメーター

startIndex
Int32

文字を削除する際の 0 から始まる開始位置。

戻り値

この文字列から対象となる文字を取り除いた新しい文字列。

例外

startIndex が 0 未満です。

または

startIndex が、この文字列内にはない位置を指定しています。

Removeメソッドの例を次に示します。 次から最後のケースでは、指定したインデックスから文字列の末尾までのすべてのテキストが削除されます。 最後のケースでは、指定したインデックスから 3 文字を削除します。

C#
// This example demonstrates the String.Remove() method.
using System;

class Sample
{
    public static void Main()
    {
        string s = "abc---def";

        Console.WriteLine("Index: 012345678");
        Console.WriteLine("1)     {0}", s);
        Console.WriteLine("2)     {0}", s.Remove(3));
        Console.WriteLine("3)     {0}", s.Remove(3, 3));
    }
}
/*
This example produces the following results:

Index: 012345678
1)     abc---def
2)     abc
3)     abcdef

*/

注釈

.NET Frameworkでは、文字列は 0 から始まります。 パラメーターの値の startIndex 範囲は、0 から文字列インスタンスの長さより 1 未満です。

注意

このメソッドは、現在のインスタンスの値を変更しません。 代わりに、元の文字列の位置 startIndex から末尾までのすべての文字が削除された新しい文字列を返します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Remove(Int32, Int32)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

現在のインスタンス内の指定した位置から指定した文字数が削除された新しい文字列を返します。

C#
public string Remove (int startIndex, int count);

パラメーター

startIndex
Int32

文字を削除する際の 0 から始まる開始位置。

count
Int32

削除する文字数。

戻り値

このインスタンスから対象となる文字を取り除いた新しい文字列。

例外

startIndex または count のいずれかが 0 より小さい値です。

または

startIndexcount を加えたものが、このインスタンスの外部の位置を指定しています。

次の例では、完全な名前からミドル ネームを削除する方法を示します。

C#
using System;

public class RemoveTest
{
    public static void Main()
    {

        string name = "Michelle Violet Banks";

        Console.WriteLine("The entire name is '{0}'", name);

        // Remove the middle name, identified by finding the spaces in the name.
        int foundS1 = name.IndexOf(" ");
        int foundS2 = name.IndexOf(" ", foundS1 + 1);

        if (foundS1 != foundS2 && foundS1 >= 0)
        {
            name = name.Remove(foundS1 + 1, foundS2 - foundS1);

            Console.WriteLine("After removing the middle name, we are left with '{0}'", name);
        }
    }
}
// The example displays the following output:
//       The entire name is 'Michelle Violet Banks'
//       After removing the middle name, we are left with 'Michelle Banks'

注釈

.NET Frameworkでは、文字列は 0 から始まります。 パラメーターの値の startIndex 範囲は、0 から文字列インスタンスの長さより 1 未満です。

注意

このメソッドは、現在のインスタンスの値を変更しません。 代わりに、パラメーターで指定された文字数が削除された新しい文字列を count 返します。 文字は、 で指定された位置で startIndex削除されます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0