共用方式為


String.Remove 方法

定義

傳回新字串,其中刪除了目前字串中指定的字元數目。

多載

Remove(Int32)

傳回新字串,其中已刪除目前執行個體中的所有字元 (從指定位置開始到最後一個位置為止)。

Remove(Int32, Int32)

傳回新字串,其中已刪除在目前執行個體中指定位置開始之指定數目的字元。

Remove(Int32)

來源:
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

要開始刪除字元之以零為起始的位置。

傳回

新字串,除了已移除的字元以外,其餘部分都與這個字串相等。

例外狀況

startIndex 小於零。

-或-

startIndex 指定不在此字串內的位置。

範例

下列範例示範 Remove 方法。 下一個到最後一個大小寫會移除從指定索引到字串結尾的所有文字。 最後一個案例會從指定的索引中移除三個字元。

// 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中,字串是以零起始。 參數的值 startIndex 可以範圍從零到一個小於字串實例的長度。

注意

這個方法不會修改目前實例的值。 相反地,它會傳回新的字串,從位置 startIndex 到原始字串結尾的所有字元都已經移除。

另請參閱

適用於

Remove(Int32, Int32)

來源:
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

要開始刪除字元之以零為起始的位置。

count
Int32

要刪除的字元數。

傳回

新字串,除了已移除的字元以外,其餘部分都與這個執行個體相等。

例外狀況

startIndexcount 小於零。

-或-

startIndex 加上 count 指定的位置超出此執行個體。

範例

下列範例示範如何從完整名稱中移除中間名。

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中,字串是以零起始。 參數的值 startIndex 可以範圍從零到一個小於字串實例的長度。

注意

這個方法不會修改目前實例的值。 相反地,它會傳回新的字串,其中已移除 參數所 count 指定的字元數。 字元會在 所 startIndex 指定的位置移除。

另請參閱

適用於