String.Remove メソッド

定義

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

オーバーロード

名前 説明
Remove(Int32, Int32)

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

Remove(Int32)

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

Remove(Int32, Int32)

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

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

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 未満です。

-又は-

startIndex プラス count このインスタンスの外側の位置を指定します。

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

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 未満です。

Note

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

こちらもご覧ください

適用対象

Remove(Int32)

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

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

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 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 から文字列インスタンスの長さです。

Note

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

こちらもご覧ください

適用対象