String.Remove メソッド

定義

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

オーバーロード

Remove(Int32)

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

Remove(Int32, Int32)

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

Remove(Int32)

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

public:
 System::String ^ Remove(int startIndex);
public string Remove (int startIndex);
member this.Remove : int -> string
Public Function Remove (startIndex As Integer) As String

パラメーター

startIndex
Int32

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

戻り値

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

例外

startIndex が 0 未満です。

または

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

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

// This example demonstrates the String.Remove() method.
using namespace System;
int 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

*/
// 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

*/
// This example demonstrates the String.Remove() method.
let s = "abc---def"

printfn "Index: 012345678"
printfn $"1)     {s}"
printfn $"2)     {s.Remove 3}"
printfn $"3)     {s.Remove(3, 3)}"
(*
This example produces the following results:

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

*)
' This example demonstrates the String.Remove() method.
Class Sample
   Public Shared Sub Main()
      Dim s As String = "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))
   End Sub
End Class
'
'This example produces the following results:
'
'Index: 012345678
'1)     abc---def
'2)     abc
'3)     abcdef
'

注釈

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

注意

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

こちらもご覧ください

適用対象

Remove(Int32, Int32)

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

public:
 System::String ^ Remove(int startIndex, int count);
public string Remove (int startIndex, int count);
member this.Remove : int * int -> string
Public Function Remove (startIndex As Integer, count As Integer) As String

パラメーター

startIndex
Int32

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

count
Int32

削除する文字数。

戻り値

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

例外

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

または

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

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

using namespace System;
int 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 middle of 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'
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'
let name = "Michelle Violet Banks"

printfn $"The entire name is '{name}'"

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

if foundS1 <> foundS2 && foundS1 >= 0 then
    let name = name.Remove(foundS1 + 1, foundS2 - foundS1)

    printfn $"After removing the middle name, we are left with '{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'
Public Class RemoveTest
    
    Public Shared Sub Main()
        Dim name As String = "Michelle Violet Banks"
                
        Console.WriteLine("The entire name is '{0}'", name)
        Dim foundS1 As Integer = name.IndexOf(" ")
        Dim foundS2 As Integer = name.IndexOf(" ", foundS1 + 1)
        If foundS1 <> foundS2 And foundS1 >= 0 Then
            
            ' remove the middle name, identified by finding the spaces in the middle of the name...    
            name = name.Remove(foundS1 + 1, foundS2 - foundS1)
            
            Console.WriteLine("After removing the middle name, we are left with '{0}'", name)
        End If
    End Sub
End Class 
' 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削除されます。

こちらもご覧ください

適用対象