String.Remove 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个新字符串,其中从当前字符串中删除指定数量的字符。
重载
| 名称 | 说明 |
|---|---|
| Remove(Int32, Int32) |
返回一个新字符串,其中从指定位置开始的当前实例中的指定字符数已被删除。 |
| Remove(Int32) |
返回一个新字符串,其中当前实例中的所有字符(从指定位置开始并继续执行最后一个位置)已被删除。 |
Remove(Int32, Int32)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- 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
要删除的字符数。
返回
与此实例等效的新字符串,但删除的字符除外。
例外
示例
以下示例演示如何从完整名称中删除中间名称。
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位置删除。
另请参阅
- Int32
- Concat(Object)
- Insert(Int32, String)
- Join(String, String[])
- Replace(Char, Char)
- Split(Char[])
- Substring(Int32)
- Trim(Char[])
适用于
Remove(Int32)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- 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
开始删除字符的从零开始的位置。
返回
与此字符串等效的新字符串,但删除的字符除外。
例外
示例
以下示例演示了该方法 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 Framework 中,字符串从零开始。 参数的值 startIndex 可以介于零到字符串实例的长度之间。
注释
此方法不会修改当前实例的值。 而是返回一个新字符串,其中所有字符从位置 startIndex 到原始字符串的末尾已被删除。
另请参阅
- Int32
- Concat(Object)
- Insert(Int32, String)
- Join(String, String[])
- Replace(Char, Char)
- Split(Char[])
- Substring(Int32)
- Trim(Char[])