String.Removeとは? わかりやすく解説

String.Remove メソッド (Int32)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定され位置から終端位置までのすべての文字をこの文字列から削除します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentOutOfRangeException

startIndex が 0 未満です。

または

startIndex に、この文字列範囲外位置指定されています。

使用例使用例

Remove メソッドコード例次に示します2 番目の例では、指定したインデックス位置から文字列終端までのテキストをすべて削除してます。3 番目の例では、指定したインデックス位置から 3 文字削除してます。

' This example demonstrates the String.Remove() method.
Imports System

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 'Main
End Class 'Sample
'
'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.
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.
import System.*;

class Sample
{
    public static void main(String[]
 args)
    {
        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));
    } //main
} //Sample
/*
This example produces the following results:

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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Remove メソッド (Int32, Int32)

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

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentOutOfRangeException

startIndex または count が 0 未満です。

または

countstartIndex足した数が、このインスタンス内にない位置示してます。

解説解説

次は、"123456" を出力する C#コード例です。

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

import System.*;

public class RemoveTest
{
    public static void main(String[]
 args)
    {
        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);
        }
    } //main
} //RemoveTest
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();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Remove メソッド




英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「String.Remove」の関連用語

String.Removeのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



String.Removeのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS