String.Remove Method

Definition

返回一个新字符串,它相当于从当前字符串删除了指定数量的字符。Returns a new string in which a specified number of characters from the current string are deleted.

Overloads

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

Parameters

startIndex
Int32

开始删除字符的从零开始的位置。The zero-based position to begin deleting characters.

Returns

String

一个新字符串,除所删除的字符之外,该字符串与此字符串等效。A new string that is equivalent to this string except for the removed characters.

Exceptions

startIndex 小于零。startIndex is less than zero.

-或--or-

startIndex 指定不在此字符串中的位置。startIndex specifies a position that is not within this string.

Examples

下面的示例演示 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
'

Remarks

.NET Framework.NET Framework中,字符串是从零开始的。In the .NET Framework.NET Framework, strings are zero-based. startIndex 参数的值的范围可以介于0到字符串实例的长度之间。The value of the startIndex parameter can range from zero to one less than the length of the string instance.

Note

此方法不会修改当前实例的值。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.

See also

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

Parameters

startIndex
Int32

开始删除字符的从零开始的位置。The zero-based position to begin deleting characters.

count
Int32

要删除的字符数。The number of characters to delete.

Returns

String

一个新字符串,除所删除的字符之外,该字符串与此实例等效。A new string that is equivalent to this instance except for the removed characters.

Exceptions

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

-或--or-

startIndexcount 指定此实例之外的位置。startIndex plus count specify a position outside this instance.

Examples

下面的示例演示如何删除完整名称中的中间名。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'

Remarks

.NET Framework.NET Framework中,字符串是从零开始的。In the .NET Framework.NET Framework, strings are zero-based. startIndex 参数的值的范围可以介于0到字符串实例的长度之间。The value of the startIndex parameter can range from zero to one less than the length of the string instance.

Note

此方法不会修改当前实例的值。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.

See also

Applies to