Share via


String.Remove メソッド

このインスタンス内の指定位置から指定した数の文字を削除します。

Public Function Remove( _
   ByVal startIndex As Integer, _   ByVal count As Integer _) As String
[C#]
public string Remove(intstartIndex,intcount);
[C++]
public: String* Remove(intstartIndex,intcount);
[JScript]
public function Remove(
   startIndex : int,count : int) : String;

パラメータ

  • startIndex
    このインスタンス内で、文字の削除を開始する位置。
  • count
    削除する文字数。

戻り値

このインスタンスと等価であり、count の数の文字が削除されている新しい String

例外

例外の種類 条件
ArgumentOutOfRangeException startIndex または count が 0 未満です。

または

startIndex と count を合計した値が、このインスタンスの長さより大きい値です。

解説

たとえば、次に示す C# コードを実行すると、"123456" と印刷されます。

String s = "123abc456";

Console.WriteLine(s.Remove(3, 3));

使用例

氏名 (名、ミドル ネーム、姓) からミドル ネームを削除する方法については、次のコード例を参照してください。

 
Imports System

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 'Main
End Class 'RemoveTest

[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 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);
        }
    }
}

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

using namespace System;

int main()
{
   String* name = S"Michelle Violet Banks";

   Console::WriteLine(S"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(S" ");
   int foundS2 = name->IndexOf(S" ", foundS1 + 1);

   if (foundS1 != foundS2 && foundS1 >= 0)
   {
      name = name->Remove(foundS1 + 1, foundS2 - foundS1);
      Console::WriteLine(S"After removing the middle name, we are left with '{0}'", name);
   }
}

[JScript] 
import System;

public class RemoveTest {
    public static function Main() : void  {

        var name : String = "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...
        var foundS1 : int = name.IndexOf(" ");
        var foundS2 : int = 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);
        }
    }
}
RemoveTest.Main();

必要条件

プラットフォーム: 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 名前空間 | Int32 | Concat | Insert | Join | Replace | Split | Substring | Trim