String.Remove 方法

定義

傳回新字串,其中刪除了目前字串中指定的字元數目。Returns a new string in which a specified number of characters from the current string are deleted.

多載

Remove(Int32)

傳回新字串,其中已刪除目前執行個體中的所有字元 (從指定位置開始到最後一個位置為止)。Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.

Remove(Int32, Int32)

傳回新字串,其中已刪除在目前執行個體中指定位置開始之指定數目的字元。Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.

Remove(Int32)

傳回新字串,其中已刪除目前執行個體中的所有字元 (從指定位置開始到最後一個位置為止)。Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.

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

要開始刪除字元之以零為起始的位置。The zero-based position to begin deleting characters.

傳回

新字串,除了已移除的字元以外,其餘部分都與這個字串相等。A new string that is equivalent to this string except for the removed characters.

例外狀況

startIndex 小於零。startIndex is less than zero.

-或--or- startIndex 指定不在此字串內的位置。startIndex specifies a position that is not within this string.

範例

下列範例示範 Remove 方法。The following example demonstrates the Remove method. 下一個至最後一個案例會從字串結尾處移除所有開頭為指定索引的文字。The next-to-last case removes all text starting from the specified index through the end of the string. 最後一種情況會從指定的索引開始移除三個字元。The last case removes three characters starting from the specified index.

// 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.
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.NET Framework中,字串是以零為基底。In the .NET Framework.NET Framework, strings are zero-based. startIndex 參數的值的範圍可以從零到一個小於字串實例的長度。The value of the startIndex parameter can range from zero to one less than the length of the string instance.

注意

這個方法不會修改目前實例的值。This method does not modify the value of the current instance. 相反地,它會傳回新的字串,其中,所有從位置 startIndex 到原始字串結尾的字元都已移除。Instead, it returns a new string in which all characters from position startIndex to the end of the original string have been removed.

另請參閱

Remove(Int32, Int32)

傳回新字串,其中已刪除在目前執行個體中指定位置開始之指定數目的字元。Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.

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

要開始刪除字元之以零為起始的位置。The zero-based position to begin deleting characters.

count
Int32

要刪除的字元數。The number of characters to delete.

傳回

新字串,除了已移除的字元以外,其餘部分都與這個執行個體相等。A new string that is equivalent to this instance except for the removed characters.

例外狀況

startIndexcount 小於零。Either startIndex or count is less than zero.

-或--or- startIndex 加上 count 指定的位置超出此執行個體。startIndex plus count specify a position outside this instance.

範例

下列範例會示範如何從完整名稱中移除中間名。The following example demonstrates how you can remove the middle name from a complete 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 );
   }
}
// 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 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'
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.NET Framework中,字串是以零為基底。In the .NET Framework.NET Framework, strings are zero-based. startIndex 參數的值的範圍可以從零到一個小於字串實例的長度。The value of the startIndex parameter can range from zero to one less than the length of the string instance.

注意

這個方法不會修改目前實例的值。This method does not modify the value of the current instance. 相反地,它會傳回新字串,其中已移除 count 參數所指定的字元數。Instead, it returns a new string in which the number of characters specified by the count parameter have been removed. 字元會在 startIndex所指定的位置移除。The characters are removed at the position specified by startIndex.

另請參閱

適用於