String.Remove 方法

定義

回傳一個新字串,刪除當前字串中指定數量的字元。

多載

名稱 Description
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

零基位置開始刪除角色。

count
Int32

要刪除的字元數量。

傳回

一個新字串,與此實例相同,但移除的字元除外。

例外狀況

startIndexcount 或 都小於零。

-或-

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 框架中,字串是零基的。 參數的值 startIndex 可以從零到比字串實例長度少一不等。

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

零基位置開始刪除角色。

傳回

一個新字串,與此字串相同,但移除的字元除外。

例外狀況

startIndex 小於零。

-或-

startIndex 大於此實例的長度。

範例

以下範例示範此 Remove 方法。 倒數第二種情況則移除從指定索引到字串末尾的所有文字。 最後一種情況是從指定索引中移除三個字元。

// 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 框架中,字串是零基的。 參數的 startIndex 值範圍可從零到字串實例的長度。

Note

此方法不會修改當前實例的值。 取而代之的是,它會回傳一個新字串,該字串中從位置 startIndex 到原字串末尾的所有字元都已被移除。

另請參閱

適用於